- 可重入互斥锁: Lock lock = new ReentrantLock()
lock.lock();
...
lock.unlock();
- 信号量: Semaphore semaphore = new Semaphore(3);
semaphore.acquire();
...
semaphore.release();
(ps:在同一时刻,可以有3个线程获得锁)
- 读写锁:ReadWriteLock lock = new ReentrantReadWriteLock();
(ps:读锁会等待写锁释放,可以同时让多个线程持有)
- 倒计数锁: CountDownLatch latch = new CountDownLatch(4);
- 栅栏锁:CuclicBarrier barrier = new CyclicBarrier()
- 死锁的预防
- 每个线程按照特定的顺序申请锁
(本随笔为观看 刘欣老师 讲解视频学习笔记)
时间: 2024-10-04 23:59:43