ReentrantLock、CountDownLatch、CyclicBarrier简介
程序员文章站
2022-05-22 12:33:57
...
ReentrantLock、CountDownLatch、CyclicBarrier
1、ReentrantLock可重入互斥锁
公平锁与非公平锁的释放锁步骤是一致的。获取锁的过程不一致,非公平锁是让当前线程优先独占,而公平锁则是让等待时间最长的线程优先,非公平的可能让其他线程没机会执行,而公平的则可以让等待时间最长的先执行,但是性能上会差点。
2、CountDownLatch是一个线程(或者多个线程)等待(await)多个线程(countDown),当多个线程都countDown完,await线程继续往后执行。
3、CyclicBarrier是多个线程相互等待(await),当所有都await后一起继续往后执行。
ReentrantLock Demo
class ReentrantLockDemo {
private final ReentrantLock lock = new ReentrantLock();
public void m() {
lock.lock();
try {
// ... method body
} finally {
lock.unlock()
}
}
}
CountDownLatch Demo
public class CountDownLatchDemo {
public static void main(String[] args){
final CountDownLatch latch = new CountDownLatch(10);
ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 10, 200, TimeUnit.MILLISECONDS,
new ArrayBlockingQueue<Runnable>(5));
for(int i=0;i<10;i++){
TestTask task=new TestTask("task_" + (i+1),latch);
executor.execute(task);
}
try {
System.out.println("等待10个子线程执行完毕...");
latch.await();
System.out.println("10个子线程已经执行完毕");
System.out.println("继续执行主线程");
}catch(Exception e){
e.printStackTrace();
}
executor.shutdown();
}
}
class TestTask implements Runnable{
private String name;
private CountDownLatch latch;
public TestTask(String name,CountDownLatch latch){
this.name=name;
this.latch=latch;
}
@Override
public void run() {
System.out.println(name + " start running.");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(name + " start end.");
this.latch.countDown();
}
}
CyclicBarrier Demo
public class CyclicBarrierDemo {
public static void main(String[] args){
CyclicBarrier barrier = new CyclicBarrier(10);
ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 10, 200, TimeUnit.MILLISECONDS,
new ArrayBlockingQueue<Runnable>(5));
for(int i=0;i<10;i++){
TestTask task=new TestTask("客人_" + (i+1),barrier,i);
executor.execute(task);
}
}
}
class TestTask implements Runnable{
private String name;
private CyclicBarrier barrier;
private int index;
public TestTask(String name,CyclicBarrier barrier,int index){
this.name=name;
this.barrier=barrier;
this.index=index;
}
@Override
public void run() {
try {
Thread.sleep(1000 + this.index*1000);
System.out.println(name + " 哥已到");
Thread.sleep(1000 + this.index*2000);
System.out.println(name + " 哥已就坐,其他几个哥赶快啊,哥饿啦.");
this.barrier.await();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(name + "终于都到啦,开吃啦");
}
}
上一篇: 线程池原理
下一篇: Java集合--Set集合接口
推荐阅读
-
并发编程CountDownLatch,CyclicBarrier,Semaphore实现原理分析
-
并发编程CountDownLatch,CyclicBarrier,Semaphore实现原理分析
-
Java并发——结合CountDownLatch源码、Semaphore源码及ReentrantLock源码来看AQS原理
-
CountDownLatch(闭锁)、Semaphore(信号量)、CyclicBarrier
-
并发编程(二)—— CountDownLatch、CyclicBarrier和Semaphore
-
JUC——线程同步辅助工具类(Semaphore,CountDownLatch,CyclicBarrier)
-
java.util.concurrent常用类(CountDownLatch,Semaphore,CyclicBarrier,Future)
-
CountDownLatch和CyclicBarrier的应用场景
-
JAVA并发编程(三):同步的辅助类之闭锁(CountDownLatch)与循环屏障(CyclicBarrier)
-
JUC 中的多线程协作工具类:CountDownLatch 和 CyclicBarrier