1.demo代码: |
package cn.yb.thread;
import java.util.concurrent.locks.ReentrantLock;
/**
* 可重入锁演示
*
* @author yb
*
*/
public class ReentrantLockDemo {
public static void main(String[] args) {
ReentrantLock lock = new ReentrantLock();
for (int i = 0; i < 10; i++) {
lock.lock();
System.out.println("加锁次数:" + (i + 1));
}
for (int i = 0; i < 10; i++) {
try {
System.out.println("解锁锁次数:" + (i + 1));
} finally {
lock.unlock();
}
}
}
}
2.运行效果: |