java 对 ReentrantLock 的 lockInterruptably 的实际使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17811544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Actual use of lockInterruptibly for a ReentrantLock
提问by Rollerball
采纳答案by Marko Topolnik
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.
逻辑与所有可中断阻塞方法相同:它允许线程立即对interrupt
从另一个线程发送给它的信号做出反应。
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.
如何使用此特定功能取决于应用程序设计。例如,它可用于杀死池中所有等待获取锁的线程。
回答by Evgeniy Dorofeev
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()
如果锁已被另一个线程持有,则可能会阻塞,并会等待直到获得锁。这与常规lock()
. 但是如果另一个线程中断等待线程 lockInterruptibly()
将抛出InterruptedException
。
回答by jatin Goyal
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
回答by Hearen
Based on Evgeniy Dorofeev's answer, I just deliberatelycome up with such demo but I really I have no clue where is exactly, it could be used. Perhaps this demo could help a tad :)
基于Evgeniy Dorofeev 的回答,我只是故意提出这样的演示,但我真的不知道确切在哪里,可以使用它。也许这个演示可以帮助一点:)
private static void testReentrantLock() {
ReentrantLock lock = new ReentrantLock();
Thread thread = new Thread(() -> {
int i = 0;
System.out.println("before entering ReentrankLock block");
try {
lock.lockInterruptibly();
while (0 < 1) {
System.out.println("in the ReentrankLock block counting: " + i++);
}
} catch (InterruptedException e) {
System.out.println("ReentrankLock block interrupted");
}
});
lock.lock(); // lock first to make the lock in the thread "waiting" and then interruptible
thread.start();
thread.interrupt();
}
Output
输出
before entering ReentrankLock block
ReentrankLock block interrupted