对于关键字synchronized来说,如果一个线程在等待锁,那么结果只有两种情况,要么它获得这把锁继续执行,要么它就保持等待。而使用重入锁,则提供另外一种可能,那就是线程可以被中断。也就是在等待锁的过程中,程序可以根据需要取消对锁的请求。
public static void main(String[] args) {
IntLock r1 = new IntLock(1);
IntLock r2 = new IntLock(2);
Thread t1 = new Thread(r1);
Thread t2 = new Thread(r2);
t1.start();
t2.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
t2.interrupt();
System.out.println("Hello world!");
}
package org.example;
import java.util.concurrent.locks.ReentrantLock;
public class IntLock implements Runnable{
public static ReentrantLock lock1 = new ReentrantLock();
public static ReentrantLock lock2 = new ReentrantLock();
int lock;
public IntLock(int lock){
this.lock = lock;
}
public void run() {
try{
if(lock == 1) {
lock1.lockInterruptibly();
try{
Thread.sleep(500);
}catch (InterruptedException e){
lock2.lockInterruptibly();
}
lock2.lockInterruptibly();
} else{
lock2.lockInterruptibly();
try{
Thread.sleep(500);
}catch (InterruptedException e){
lock1.lockInterruptibly();
}
lock1.lockInterruptibly();
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
if(lock1.isHeldByCurrentThread())
lock1.unlock();
if(lock2.isHeldByCurrentThread())
lock2.unlock();
System.out.println(Thread.currentThread().getId()+":线程退出");
}
}
}
13:线程退出
12:线程退出
Exception in thread "Thread-1" java.lang.RuntimeException: java.lang.InterruptedException
at org.example.IntLock.run(IntLock.java:33)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.InterruptedException
at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireInterruptibly(AbstractQueuedSynchronizer.java:898)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireInterruptibly(AbstractQueuedSynchronizer.java:1222)
at java.util.concurrent.locks.ReentrantLock.lockInterruptibly(ReentrantLock.java:335)
at org.example.IntLock.run(IntLock.java:30)
... 1 more
线程1和线程2启动后,t1先占用lock1,再占用lock2,再请求lock1.因此很冗余形成t1和t2之间相互等待。在这里,对锁的请求,统一使用lockInterruptibly()方法。这是一个可以对中断进行响应的锁申请动作,即在等待锁的过程中,可以响应中断。
主线程main处于休眠状态,此时,这两个线程处于死锁的状态。
由于t2线程被中断,故而t1会放弃对lock1的申请,同时释放已获得的lock2.这个操作导致t1线程可以顺利得到lock2而进行执行。
可以看到,中断后两个线程双双退出,但真正完成工作的只有t1,而t2线程则放弃其任务直接退出。释放资源。