结论:
- lock() 如果没有获取到锁,会一直阻塞并尝试获取锁,直到获取到锁。
- lock() 获取到锁之前,其他线程不可以中断该线程。因为线程Thread如线程t2的interrupt方法,想要中断线程,但不会真的中断,只会把t2的中断标志改变,所以线程t2还会继续运行。
- lockInterruptibly() 获取到锁之前,其他线程可以中断该线程。因为对interrupt标志进行了处理
lock()阻塞获取锁。如果获取不到锁,会一直阻塞,直到获取到锁
代码:
static void main(String[] args) {
ReentrantLock reentrantLock = new ReentrantLock();
// t1先获取锁
Thread t1 = new Thread(()->{
try {
reentrantLock.lock();
System.out.println("t1获取到锁");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
System.out.println("t1报错");
throw new RuntimeException(e);
}
} finally {
reentrantLock.unlock();
}
System.out.println("11释放锁");
});
System.out.println("t1 name :"+t1.getName());
t1.start();
// 测试重点。t2后获取锁。测试lock、lockInterruptibly方法
Thread t2 = new Thread(()->{
try {
// 确保t先执行性
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("t2报错");
throw new RuntimeException(e);
}
try {
long l = System.currentTimeMillis();
reentrantLock.lock();
long l1 = System.currentTimeMillis();
System.out.println("t2等待获取锁的时间"+(l1-l));
} finally {
reentrantLock.unlock();
}
});
System.out.println("t2 name :"+t2.getName());
t2.start();
}
结果:
lockInterruptibly()。获取锁前该线程可以被中断。
代码:在上面代码的基础上,lock换成lockInterruptibly,加了个t3线程(对t2进行中断操作)
static void main(String[] args) {
ReentrantLock reentrantLock = new ReentrantLock();
// t1先获取锁
Thread t1 = new Thread(()->{
try {
reentrantLock.lock();
System.out.println("t1获取到锁");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
System.out.println("t1报错");
throw new RuntimeException(e);
}
} finally {
reentrantLock.unlock();
}
System.out.println("11释放锁");
});
System.out.println("t1 name :"+t1.getName());
t1.start();
// 测试重点。t2后获取锁。测试lock、lockInterruptibly方法
Thread t2 = new Thread(()->{
try {
// 确保t先执行性
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("t2报错");
throw new RuntimeException(e);
}
try {
long l = System.currentTimeMillis();
// reentrantLock.lock();
reentrantLock.lockInterruptibly();
long l1 = System.currentTimeMillis();
System.out.println("t2等待获取锁的时间"+(l1-l));
} catch (InterruptedException e) {
System.out.println("t2获取锁失败,t2需要中断,t2要退出啦");
throw new RuntimeException(e);
} finally {
reentrantLock.unlock();
}
});
System.out.println("t2 name :"+t2.getName());
t2.start();
// 用来中断t2的
Thread t3 = new Thread(()->{
try {
Thread.sleep(2000);
} catch (Exception e) {
System.out.println("t3报错");
throw new RuntimeException(e);
}
t2.interrupt();
System.out.println("t3中断t2了");
});
System.out.println("t3 name :"+t3.getName());
t3.start();
}
结果:
如果t2没用lockInterruptibly(),而是用lock()。t3中断t2是中断不了的,因为interrupt方法只是打个标记,不会真的去中断方法。lock()针对这种打标记没做处理,所以t2会继续运行
结果如下: