线程同步方法

1.synchronized
2.ReentrantLock(可重入锁)
public class Main {
    ReentrantLock lock = new ReentrantLock();
    private ThreadLocal stream = new ThreadLocal();

    public static void main(String[] args) {
//        test();
    }

    public void test() {

        lock.lock();

        lock.unlock();
    }
}

3.Semaphore

public class Main {
    static Semaphore semaphore = new Semaphore(2);

    public static void main(String[] args) throws InterruptedException {
        try {
            semaphore.acquire();
            //执行访问逻辑
        } finally {
            semaphore.release();
        }
    }
}
posted @ 2024-08-15 15:30  辉辉、  阅读(14)  评论(0)    收藏  举报