CountDownLatch用法

CountDownLatch类位于java.util.concurrent包下,利用它可以实现类似计数器的功能。比如有一个任务A,它要等待其他4个任务执行完毕之后才能执行,此时就可以利用CountDownLatch来实现这种功能了。

CountDownLatch类只提供了一个构造器:

1   public CountDownLatch(int count) {  };  //参数count为计数值

下面这3个方法是CountDownLatch类中最重要的方法:

1   public void await() throws InterruptedException { };  
2    //调用await()方法的线程会被挂起,它会等待直到count值为0才继续执行
3   public boolean await(long timeout, TimeUnit unit) throws InterruptedException { }; 
     //和await()类似,只不过等待一定的时间后count值还没变为0的话就会继续执行
4   public void countDown() { };  //将count值减1

下面看一个例子大家就清楚CountDownLatch的用法了:

1   public class Test {
2        public static void main(String[] args) {   
3            final CountDownLatch latch = new CountDownLatch(2);
4    
5            new Thread(){
6                public void run() {
7                    try {
8                        System.out.println("子线程"+Thread.currentThread().getName()+"正在执行");
9   
10  
11                      Thread.sleep(3000);
12                      System.out.println("子线程"+Thread.currentThread().getName()+"执行完毕");
13                      latch.countDown();
14                  } catch (InterruptedException e) {
15                      e.printStackTrace();
16                  }
17               };
18           }.start();
19   
20           new Thread(){
21               public void run() {
22                   try {
23                       System.out.println("子线程"+Thread.currentThread().getName()+"正在执行");
24                       Thread.sleep(3000);
25                       System.out.println("子线程"+Thread.currentThread().getName()+"执行完毕");
26                       latch.countDown();
27                  } catch (InterruptedException e) {
28                      e.printStackTrace();
29                  }
30               };
31           }.start();
32   
33           try {
34               System.out.println("等待2个子线程执行完毕...");
35              latch.await();
36              System.out.println("2个子线程已经执行完毕");
37              System.out.println("继续执行主线程");
38          } catch (InterruptedException e) {
39              e.printStackTrace();
40          }
         }
    }

执行结果:

1   线程Thread-0正在执行
2   线程Thread-1正在执行
3   等待2个子线程执行完毕...
4   线程Thread-0执行完毕
5   线程Thread-1执行完毕
6   2个子线程已经执行完毕
7   继续执行主线程
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CountDownLatch是Java中提供的一个同步工具类,它可以用来控制一个或多个线程等待多个线程的操作完成。 CountDownLatch用法如下: 1. 创建一个CountDownLatch对象,指定需要等待的线程数。 ``` CountDownLatch latch = new CountDownLatch(5); ``` 2. 每个需要等待的线程执行完后调用CountDownLatchcountDown方法减少计数器。 ``` latch.countDown(); ``` 3. 等待所有线程执行完毕后,调用CountDownLatch的await方法进行等待。 ``` latch.await(); ``` 4. 在所有线程执行完毕后,主线程或其他线程可以继续执行自己的操作。 示例代码: ``` import java.util.concurrent.CountDownLatch; public class CountDownLatchDemo { public static void main(String[] args) throws InterruptedException { CountDownLatch latch = new CountDownLatch(5); for (int i = 0; i < 5; i++) { new Thread(() -> { System.out.println(Thread.currentThread().getName() + " is working..."); try { // 模拟线程执行时间 Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + " done"); latch.countDown(); }).start(); } System.out.println("Waiting for all threads to finish..."); latch.await(); System.out.println("All threads have finished, continue to execute the main thread."); } } ``` 运行结果: ``` Waiting for all threads to finish... Thread-0 is working... Thread-2 is working... Thread-1 is working... Thread-3 is working... Thread-4 is working... Thread-1 done Thread-3 done Thread-0 done Thread-2 done Thread-4 done All threads have finished, continue to execute the main thread. ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值