0
点赞
收藏
分享

微信扫一扫

Java ReentrantLock

佛贝鲁先生 2022-03-23 阅读 51
java

1. 锁的意义——线程同步

2. 案例——基于ReentrantLock来实现同步

public class A {

    public static ReentrantLock reentrantLock = new ReentrantLock();

    public static void main(String[] args) {
        new Thread(() -> {
            testSync();
        }, "t1").start();

        new Thread(() -> {
            testSync();
        }, "t2").start();


    }

    public static void testSync(){

        reentrantLock.lock();
        try {
            System.out.println(Thread.currentThread().getName());
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            reentrantLock.unlock();
        }
    }
}

3. ReentrantLock跟Synchronized的区别?

4. ReentrantLock理解?

5. 核心

举报

相关推荐

0 条评论