多线程可重入锁

在这里插入图片描述

synchronized写法

在这里插入图片描述

Lock写法

public class TestReentrantLock {
    public static void main(String[] args) {
        Lock lock=new ReentrantLock();
        new Thread(()->{
            lock.lock();
            try {
                System.out.println(Thread.currentThread().getName()+"外层");
                lock.lock();
                try {
                    System.out.println(Thread.currentThread().getName()+"内层");
                }finally {
                    lock.unlock();
                }
            }finally {
                lock.unlock();
            }
        },"t1").start();
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_44371237/article/details/121769110