public class ReentrantLockTest implements Runnable {
public static ReentrantLock lock = new ReentrantLock();
public static int i = 0;
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "启动!");
lock.lock(); // 看这里就可以
System.out.println(Thread.currentThread().getName() + "获取锁:" + lock.getHoldCount());
try {
i++;
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock(); // 看这里就可以
System.out.println(Thread.currentThread().getName() + "释放锁" + lock.getHoldCount());
}
}
public static void main(String[] args) throws InterruptedException {
ReentrantLockTest test = new ReentrantLockTest();
Thread t1 = new Thread(test);
Thread t2 = new Thread(test);
t1.start();
t2.start();
t1.join();
t2.join(); // main线程会等待t1和t2都运行完再执行以后的流程
System.err.println(i);
}
}
lock()获取锁
- 如果没有其它线程持有此锁,当前线程获取锁,holdCount = 1,立即返回
- 如果当前线程已经持有此锁,holdCount++,立即返回
- 如果其它线程持锁,当前线程挂起等待线程调度,获取到锁,holdCount = 1
public void lock() {
sync.lock();
}
lockInterruptibly() 获取锁,直到当前线程被中断
-
如果没有其它线程持有此锁,当前线程获取锁,holdCount = 1,立即返回
-
如果当前线程已经持有此锁,holdCount++,立即返回
-
如果其它线程持锁,当前线程挂起等待线程调度,
条件1:获取到锁,holdCount = 1 条件2:其它线程中断了当前线程,抛出一个InterruptedException
*
* <p>In this implementation, as this method is an explicit
* interruption point, preference is given to responding to the
* interrupt over normal or reentrant acquisition of the lock.
public void lockInterruptibly() throws InterruptedException {
sync.acquireInterruptibly(1);
}
-
lock方法会忽略中断请求,继续获取锁直到成功,才响应中断;
-
lockInterruptibly则直接抛出中断异常来立即响应中断,由上层调用者处理中断。