线程同步方法
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();
}
}
}
每天坚持,终会抵达!

浙公网安备 33010602011771号