Actual use of lockInterruptibly for a ReentrantLock
What do you actually use for this method lockInterruptibly
? I have read the API however it's not very clear to me. Could anybody express it in other words?
java concurrency locking reentrantlock
add a comment |
What do you actually use for this method lockInterruptibly
? I have read the API however it's not very clear to me. Could anybody express it in other words?
java concurrency locking reentrantlock
add a comment |
What do you actually use for this method lockInterruptibly
? I have read the API however it's not very clear to me. Could anybody express it in other words?
java concurrency locking reentrantlock
What do you actually use for this method lockInterruptibly
? I have read the API however it's not very clear to me. Could anybody express it in other words?
java concurrency locking reentrantlock
java concurrency locking reentrantlock
asked Jul 23 '13 at 13:26
RollerballRollerball
4,6811665120
4,6811665120
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
The logic is the same as for all interruptible blocking methods: it allows the thread to immediately react to the interrupt
signal sent to it from another thread.
How this particular feature is used is up to the application design. For example, it can be used to kill a contingent of threads in a pool which are all waiting to aquire a lock.
As stated "it allows the thread to immediately react to the interrupt signal sent to it from another thread," does it mean, When thread calls lockInterruptibly(), will wait for interrupt signal from another thread unless lock is immediatley available to get hold of it?. The thread that sends interrupt signal holds the lock untill it signals interrupt?
– MKod
Dec 21 '14 at 9:40
Any thread can interrupt the thread, it doesn't need to be the one holding the lock.
– Marko Topolnik
Dec 21 '14 at 20:09
Java thread interrupts differ from hardware or operating system interrupts. Interrupt delivery is synchronous & non-preemptive rather than asynchronous & preemptive i.e., they don’t occur at an arbitrary point & don’t pause (& later resume) running code. A program must test for them explicitly. So, are you saying that,lockInterruptibly
continuously tests for them explicitly?
– overexchange
Dec 19 '17 at 16:21
@overexchange It doesn't have to continuously test for them because it either returns immediately or is suspended. If another thread raises itsinterrupted
flag, it will be notified and when it resumes, it will throw theInterruptedException
– Marko Topolnik
Dec 20 '17 at 9:02
add a comment |
lockInterruptibly()
may block if the the lock is already held by another thread and will wait until the lock is aquired. This is the same as with regular lock()
. But if another thread interrupts the waiting thread lockInterruptibly()
will throw InterruptedException
.
add a comment |
Try to understand this concept through below code example.
Code Sample:
package codingInterview.thread;
import java.util.concurrent.locks.ReentrantLock;
public class MyRentrantlock
Thread t = new Thread()
@Override
public void run()
ReentrantLock r = new ReentrantLock();
r.lock();
System.out.println("lock() : lock count :" + r.getHoldCount());
interrupt();
System.out.println("Current thread is intrupted");
r.tryLock();
System.out.println("tryLock() on intrupted thread lock count :" + r.getHoldCount());
try
r.lockInterruptibly();
System.out.println("lockInterruptibly() --NOt executable statement" + r.getHoldCount());
catch (InterruptedException e)
r.lock();
System.out.println("Error");
finally
r.unlock();
System.out.println("lockInterruptibly() not able to Acqurie lock: lock count :" + r.getHoldCount());
r.unlock();
System.out.println("lock count :" + r.getHoldCount());
r.unlock();
System.out.println("lock count :" + r.getHoldCount());
;
public static void main(String str)
MyRentrantlock m = new MyRentrantlock();
m.t.start();
System.out.println("");
Output:
lock() : lock count :1
Current thread is intrupted
tryLock() on intrupted thread lock count :2
Error
lockInterruptibly() not able to Acqurie lock: lock count :2
lock count :1
lock count :0
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f17811544%2factual-use-of-lockinterruptibly-for-a-reentrantlock%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The logic is the same as for all interruptible blocking methods: it allows the thread to immediately react to the interrupt
signal sent to it from another thread.
How this particular feature is used is up to the application design. For example, it can be used to kill a contingent of threads in a pool which are all waiting to aquire a lock.
As stated "it allows the thread to immediately react to the interrupt signal sent to it from another thread," does it mean, When thread calls lockInterruptibly(), will wait for interrupt signal from another thread unless lock is immediatley available to get hold of it?. The thread that sends interrupt signal holds the lock untill it signals interrupt?
– MKod
Dec 21 '14 at 9:40
Any thread can interrupt the thread, it doesn't need to be the one holding the lock.
– Marko Topolnik
Dec 21 '14 at 20:09
Java thread interrupts differ from hardware or operating system interrupts. Interrupt delivery is synchronous & non-preemptive rather than asynchronous & preemptive i.e., they don’t occur at an arbitrary point & don’t pause (& later resume) running code. A program must test for them explicitly. So, are you saying that,lockInterruptibly
continuously tests for them explicitly?
– overexchange
Dec 19 '17 at 16:21
@overexchange It doesn't have to continuously test for them because it either returns immediately or is suspended. If another thread raises itsinterrupted
flag, it will be notified and when it resumes, it will throw theInterruptedException
– Marko Topolnik
Dec 20 '17 at 9:02
add a comment |
The logic is the same as for all interruptible blocking methods: it allows the thread to immediately react to the interrupt
signal sent to it from another thread.
How this particular feature is used is up to the application design. For example, it can be used to kill a contingent of threads in a pool which are all waiting to aquire a lock.
As stated "it allows the thread to immediately react to the interrupt signal sent to it from another thread," does it mean, When thread calls lockInterruptibly(), will wait for interrupt signal from another thread unless lock is immediatley available to get hold of it?. The thread that sends interrupt signal holds the lock untill it signals interrupt?
– MKod
Dec 21 '14 at 9:40
Any thread can interrupt the thread, it doesn't need to be the one holding the lock.
– Marko Topolnik
Dec 21 '14 at 20:09
Java thread interrupts differ from hardware or operating system interrupts. Interrupt delivery is synchronous & non-preemptive rather than asynchronous & preemptive i.e., they don’t occur at an arbitrary point & don’t pause (& later resume) running code. A program must test for them explicitly. So, are you saying that,lockInterruptibly
continuously tests for them explicitly?
– overexchange
Dec 19 '17 at 16:21
@overexchange It doesn't have to continuously test for them because it either returns immediately or is suspended. If another thread raises itsinterrupted
flag, it will be notified and when it resumes, it will throw theInterruptedException
– Marko Topolnik
Dec 20 '17 at 9:02
add a comment |
The logic is the same as for all interruptible blocking methods: it allows the thread to immediately react to the interrupt
signal sent to it from another thread.
How this particular feature is used is up to the application design. For example, it can be used to kill a contingent of threads in a pool which are all waiting to aquire a lock.
The logic is the same as for all interruptible blocking methods: it allows the thread to immediately react to the interrupt
signal sent to it from another thread.
How this particular feature is used is up to the application design. For example, it can be used to kill a contingent of threads in a pool which are all waiting to aquire a lock.
answered Jul 23 '13 at 13:55
Marko TopolnikMarko Topolnik
147k19198325
147k19198325
As stated "it allows the thread to immediately react to the interrupt signal sent to it from another thread," does it mean, When thread calls lockInterruptibly(), will wait for interrupt signal from another thread unless lock is immediatley available to get hold of it?. The thread that sends interrupt signal holds the lock untill it signals interrupt?
– MKod
Dec 21 '14 at 9:40
Any thread can interrupt the thread, it doesn't need to be the one holding the lock.
– Marko Topolnik
Dec 21 '14 at 20:09
Java thread interrupts differ from hardware or operating system interrupts. Interrupt delivery is synchronous & non-preemptive rather than asynchronous & preemptive i.e., they don’t occur at an arbitrary point & don’t pause (& later resume) running code. A program must test for them explicitly. So, are you saying that,lockInterruptibly
continuously tests for them explicitly?
– overexchange
Dec 19 '17 at 16:21
@overexchange It doesn't have to continuously test for them because it either returns immediately or is suspended. If another thread raises itsinterrupted
flag, it will be notified and when it resumes, it will throw theInterruptedException
– Marko Topolnik
Dec 20 '17 at 9:02
add a comment |
As stated "it allows the thread to immediately react to the interrupt signal sent to it from another thread," does it mean, When thread calls lockInterruptibly(), will wait for interrupt signal from another thread unless lock is immediatley available to get hold of it?. The thread that sends interrupt signal holds the lock untill it signals interrupt?
– MKod
Dec 21 '14 at 9:40
Any thread can interrupt the thread, it doesn't need to be the one holding the lock.
– Marko Topolnik
Dec 21 '14 at 20:09
Java thread interrupts differ from hardware or operating system interrupts. Interrupt delivery is synchronous & non-preemptive rather than asynchronous & preemptive i.e., they don’t occur at an arbitrary point & don’t pause (& later resume) running code. A program must test for them explicitly. So, are you saying that,lockInterruptibly
continuously tests for them explicitly?
– overexchange
Dec 19 '17 at 16:21
@overexchange It doesn't have to continuously test for them because it either returns immediately or is suspended. If another thread raises itsinterrupted
flag, it will be notified and when it resumes, it will throw theInterruptedException
– Marko Topolnik
Dec 20 '17 at 9:02
As stated "it allows the thread to immediately react to the interrupt signal sent to it from another thread," does it mean, When thread calls lockInterruptibly(), will wait for interrupt signal from another thread unless lock is immediatley available to get hold of it?. The thread that sends interrupt signal holds the lock untill it signals interrupt?
– MKod
Dec 21 '14 at 9:40
As stated "it allows the thread to immediately react to the interrupt signal sent to it from another thread," does it mean, When thread calls lockInterruptibly(), will wait for interrupt signal from another thread unless lock is immediatley available to get hold of it?. The thread that sends interrupt signal holds the lock untill it signals interrupt?
– MKod
Dec 21 '14 at 9:40
Any thread can interrupt the thread, it doesn't need to be the one holding the lock.
– Marko Topolnik
Dec 21 '14 at 20:09
Any thread can interrupt the thread, it doesn't need to be the one holding the lock.
– Marko Topolnik
Dec 21 '14 at 20:09
Java thread interrupts differ from hardware or operating system interrupts. Interrupt delivery is synchronous & non-preemptive rather than asynchronous & preemptive i.e., they don’t occur at an arbitrary point & don’t pause (& later resume) running code. A program must test for them explicitly. So, are you saying that,
lockInterruptibly
continuously tests for them explicitly?– overexchange
Dec 19 '17 at 16:21
Java thread interrupts differ from hardware or operating system interrupts. Interrupt delivery is synchronous & non-preemptive rather than asynchronous & preemptive i.e., they don’t occur at an arbitrary point & don’t pause (& later resume) running code. A program must test for them explicitly. So, are you saying that,
lockInterruptibly
continuously tests for them explicitly?– overexchange
Dec 19 '17 at 16:21
@overexchange It doesn't have to continuously test for them because it either returns immediately or is suspended. If another thread raises its
interrupted
flag, it will be notified and when it resumes, it will throw the InterruptedException
– Marko Topolnik
Dec 20 '17 at 9:02
@overexchange It doesn't have to continuously test for them because it either returns immediately or is suspended. If another thread raises its
interrupted
flag, it will be notified and when it resumes, it will throw the InterruptedException
– Marko Topolnik
Dec 20 '17 at 9:02
add a comment |
lockInterruptibly()
may block if the the lock is already held by another thread and will wait until the lock is aquired. This is the same as with regular lock()
. But if another thread interrupts the waiting thread lockInterruptibly()
will throw InterruptedException
.
add a comment |
lockInterruptibly()
may block if the the lock is already held by another thread and will wait until the lock is aquired. This is the same as with regular lock()
. But if another thread interrupts the waiting thread lockInterruptibly()
will throw InterruptedException
.
add a comment |
lockInterruptibly()
may block if the the lock is already held by another thread and will wait until the lock is aquired. This is the same as with regular lock()
. But if another thread interrupts the waiting thread lockInterruptibly()
will throw InterruptedException
.
lockInterruptibly()
may block if the the lock is already held by another thread and will wait until the lock is aquired. This is the same as with regular lock()
. But if another thread interrupts the waiting thread lockInterruptibly()
will throw InterruptedException
.
answered Jul 23 '13 at 13:59
Evgeniy DorofeevEvgeniy Dorofeev
106k23143223
106k23143223
add a comment |
add a comment |
Try to understand this concept through below code example.
Code Sample:
package codingInterview.thread;
import java.util.concurrent.locks.ReentrantLock;
public class MyRentrantlock
Thread t = new Thread()
@Override
public void run()
ReentrantLock r = new ReentrantLock();
r.lock();
System.out.println("lock() : lock count :" + r.getHoldCount());
interrupt();
System.out.println("Current thread is intrupted");
r.tryLock();
System.out.println("tryLock() on intrupted thread lock count :" + r.getHoldCount());
try
r.lockInterruptibly();
System.out.println("lockInterruptibly() --NOt executable statement" + r.getHoldCount());
catch (InterruptedException e)
r.lock();
System.out.println("Error");
finally
r.unlock();
System.out.println("lockInterruptibly() not able to Acqurie lock: lock count :" + r.getHoldCount());
r.unlock();
System.out.println("lock count :" + r.getHoldCount());
r.unlock();
System.out.println("lock count :" + r.getHoldCount());
;
public static void main(String str)
MyRentrantlock m = new MyRentrantlock();
m.t.start();
System.out.println("");
Output:
lock() : lock count :1
Current thread is intrupted
tryLock() on intrupted thread lock count :2
Error
lockInterruptibly() not able to Acqurie lock: lock count :2
lock count :1
lock count :0
add a comment |
Try to understand this concept through below code example.
Code Sample:
package codingInterview.thread;
import java.util.concurrent.locks.ReentrantLock;
public class MyRentrantlock
Thread t = new Thread()
@Override
public void run()
ReentrantLock r = new ReentrantLock();
r.lock();
System.out.println("lock() : lock count :" + r.getHoldCount());
interrupt();
System.out.println("Current thread is intrupted");
r.tryLock();
System.out.println("tryLock() on intrupted thread lock count :" + r.getHoldCount());
try
r.lockInterruptibly();
System.out.println("lockInterruptibly() --NOt executable statement" + r.getHoldCount());
catch (InterruptedException e)
r.lock();
System.out.println("Error");
finally
r.unlock();
System.out.println("lockInterruptibly() not able to Acqurie lock: lock count :" + r.getHoldCount());
r.unlock();
System.out.println("lock count :" + r.getHoldCount());
r.unlock();
System.out.println("lock count :" + r.getHoldCount());
;
public static void main(String str)
MyRentrantlock m = new MyRentrantlock();
m.t.start();
System.out.println("");
Output:
lock() : lock count :1
Current thread is intrupted
tryLock() on intrupted thread lock count :2
Error
lockInterruptibly() not able to Acqurie lock: lock count :2
lock count :1
lock count :0
add a comment |
Try to understand this concept through below code example.
Code Sample:
package codingInterview.thread;
import java.util.concurrent.locks.ReentrantLock;
public class MyRentrantlock
Thread t = new Thread()
@Override
public void run()
ReentrantLock r = new ReentrantLock();
r.lock();
System.out.println("lock() : lock count :" + r.getHoldCount());
interrupt();
System.out.println("Current thread is intrupted");
r.tryLock();
System.out.println("tryLock() on intrupted thread lock count :" + r.getHoldCount());
try
r.lockInterruptibly();
System.out.println("lockInterruptibly() --NOt executable statement" + r.getHoldCount());
catch (InterruptedException e)
r.lock();
System.out.println("Error");
finally
r.unlock();
System.out.println("lockInterruptibly() not able to Acqurie lock: lock count :" + r.getHoldCount());
r.unlock();
System.out.println("lock count :" + r.getHoldCount());
r.unlock();
System.out.println("lock count :" + r.getHoldCount());
;
public static void main(String str)
MyRentrantlock m = new MyRentrantlock();
m.t.start();
System.out.println("");
Output:
lock() : lock count :1
Current thread is intrupted
tryLock() on intrupted thread lock count :2
Error
lockInterruptibly() not able to Acqurie lock: lock count :2
lock count :1
lock count :0
Try to understand this concept through below code example.
Code Sample:
package codingInterview.thread;
import java.util.concurrent.locks.ReentrantLock;
public class MyRentrantlock
Thread t = new Thread()
@Override
public void run()
ReentrantLock r = new ReentrantLock();
r.lock();
System.out.println("lock() : lock count :" + r.getHoldCount());
interrupt();
System.out.println("Current thread is intrupted");
r.tryLock();
System.out.println("tryLock() on intrupted thread lock count :" + r.getHoldCount());
try
r.lockInterruptibly();
System.out.println("lockInterruptibly() --NOt executable statement" + r.getHoldCount());
catch (InterruptedException e)
r.lock();
System.out.println("Error");
finally
r.unlock();
System.out.println("lockInterruptibly() not able to Acqurie lock: lock count :" + r.getHoldCount());
r.unlock();
System.out.println("lock count :" + r.getHoldCount());
r.unlock();
System.out.println("lock count :" + r.getHoldCount());
;
public static void main(String str)
MyRentrantlock m = new MyRentrantlock();
m.t.start();
System.out.println("");
Output:
lock() : lock count :1
Current thread is intrupted
tryLock() on intrupted thread lock count :2
Error
lockInterruptibly() not able to Acqurie lock: lock count :2
lock count :1
lock count :0
answered Aug 21 '18 at 2:56
jatin Goyaljatin Goyal
835
835
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f17811544%2factual-use-of-lockinterruptibly-for-a-reentrantlock%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown