JAVA多线程之(CountDownLatch)

 

一个同步辅助类,它允许一个或多个线程一直等待一组其他线程执行结束后再做处理。用给定的计数初始化 CountDownLatch。由于调用了 countDown() 方法,所以在当前计数到达零之前,await 方法会一直受阻塞。之后,会释放所有等待的线程,await 的所有后续调用都将立即执行。 个线程(或者多个), 等待另外N个线程完成某个事情之后才能执行,实例代码如下

 

package nc.com.thread.other;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CountdownLatchTest {
    //命令发出器
	private static CountDownLatch orderCount= new CountDownLatch(1);
	//等待命令结束相应器
	private static CountDownLatch  answerCount = new CountDownLatch(3);
	//线程池
	private static ExecutorService service = Executors.newCachedThreadPool();
	
	public static void main(String[] args) throws Exception {
		for(int i=0;i<3;i++){
			Runnable r=new Runnable() {
				public void run() {
					System.out.println(""+Thread.currentThread().getName()+"到达目的地,等待司令发出命令...");
					try {
						orderCount.await();
						System.out.println(""+Thread.currentThread().getName()+"得到司令发出的命令,go go ....");
						Thread.sleep((long)(Math.random()*1000));	
						System.out.println(""+Thread.currentThread().getName()+"已完成司令发出的命令,归队go go ....");
						answerCount.countDown();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			} ;
			service.submit(r);
		}
		System.out.println(""+Thread.currentThread().getName()+"司令即将发出的命令");
		orderCount.countDown();
		System.out.println(""+Thread.currentThread().getName()+"司令已发出的命令,go go ....");
		answerCount.await();
		System.out.println(""+Thread.currentThread().getName()+"全部士兵已经完成司令命令归队");
		service.shutdown();
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 中,CountDownLatch 是一种同步工具,它可以用来控制线程的执行顺序或者等待其他线程完成某些操作。下面是一个使用 CountDownLatch 的示例,用于实现多线程的协调: ```java import java.util.concurrent.CountDownLatch; public class MultiThreadExample { public static void main(String[] args) throws InterruptedException { int threadCount = 5; CountDownLatch latch = new CountDownLatch(threadCount); for (int i = 0; i < threadCount; i++) { Thread thread = new Thread(new Worker(latch)); thread.start(); } System.out.println("Main thread is waiting for workers to finish."); latch.await(); System.out.println("All workers have finished. Main thread can continue."); } static class Worker implements Runnable { private final CountDownLatch latch; public Worker(CountDownLatch latch) { this.latch = latch; } @Override public void run() { try { System.out.println("Worker is performing some task."); Thread.sleep(1000); System.out.println("Worker has finished its task."); } catch (InterruptedException e) { e.printStackTrace(); } finally { latch.countDown(); } } } } ``` 在上面的示例中,我们创建了一个 CountDownLatch 对象,并将其初始化为预期的线程数量(5)。然后,在主线程中创建了 5 个 Worker 线程,并启动它们。每个 Worker 线程都会执行一些任务,然后调用 `latch.countDown()` 来通知 CountDownLatch 已经完成了一部分工作。最后,主线程调用 `latch.await()` 来等待所有 Worker 线程完成任务。 当所有 Worker 线程完成任务后,主线程就会继续执行。这样,我们就通过 CountDownLatch 来协调多线程的执行顺序了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值