CountDownLatch同步工具类的使用

同步工具类的使用大大方便了多线程并发的操作。CountDownLatch 是一个 java.util.concurrent下的同步工具类,它允许一个或多个线程一直等待,直到其他线程执行完后再执行。这种需求如果使用基本的线程通信来操作的确过于繁琐。使用CountDownLatch工具类大大提高了这类问题情形的工作效率。

例如有10个线程,要求主线程必须在其他9个线程全部执行完后才执行,不进行任何操作,执行结果肯定是杂乱无序的:

 1 import java.util.concurrent.CountDownLatch;
 2 
 3 public class CountDownLatchDemo {
 4     public static void main(String[] args) {
 5       for (int i=0;i<9;i++) {
 6           new Thread(()->{
 7               System.out.println(Thread.currentThread().getName()+"运行!");
 8               },"线程"+String.valueOf(i)).start();
 9       }
10         System.out.println("main线程结束!!!");
11     }
12 }

使用CountDownLatch工具类,及使用方法:

 1 import java.util.concurrent.CountDownLatch;
 2 
 3 public class CountDownLatchDemo {
 4     public static void main(String[] args) throws InterruptedException {
 5         CountDownLatch countDownLatch=new CountDownLatch(9);
 6       for (int i=0;i<9;i++) {
 7           new Thread(()->{
 8               System.out.println(Thread.currentThread().getName()+"运行!");
 9               countDownLatch.countDown();
10               },"线程"+String.valueOf(i)).start();
11       }
12       countDownLatch.await();
13         System.out.println("main线程结束!!!");
14     }
15 }

原理:

* CountDownLatch主要有两个方法,当一个或多个线程调用await方法时,这些线程会阻塞

* 其它线程调用countDown方法会将计数器减1(调用countDown方法的线程不会阻塞)

* 当计数器的值变为0时,因await方法阻塞的线程会被唤醒,继续执行

了解更多:

https://www.cnblogs.com/nullzx/p/5272807.html

ps:自定义模板代码,将代码块抽取成快捷键的方法:

https://blog.csdn.net/zhou520yue520/article/details/82713820

 

猜你喜欢

转载自www.cnblogs.com/fangtingfei/p/12021145.html