多线程控制类-ReentrantLock可重入锁演示

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.运行效果:

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/m0_46266503/article/details/106985849