Java ReentrantLock

阅读 51

2022-03-23

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)

0 0 举报