java多线程编程之CountDownLatch

本文通过两个具体示例详细解析了 Java 并发包中 CountDownLatch 类的使用方法,包括其核心方法 countDown 和 await 的应用,并展示了如何利用 ExecutorService 来启动多个线程并等待它们全部完成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java.util.concurrent.CountDownLatch这个类里面的主要方法为:

1.countDown(),Decrements the count of the latch, releasing all waiting threads if the count reaches zero.

2.await(),Causes the current thread to wait until the latch has counted down to zero, unless the thread is interrupted.

//Runner.java
package com.cc;
import java.util.concurrent.CountDownLatch;

public class Runner implements Runnable{
	private CountDownLatch latch;
	private int index;

	public Runner(int index){
		this.index = index;
	}
	
	public Runner(CountDownLatch latch, int index){
		this.latch = latch;
		this.index = index;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			Thread.sleep(10);
			latch.countDown();
			System.out.println("latch countDown :"+this.index);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("latch countDown:"+this.index+"end");
	}
	public CountDownLatch getLatch() {
		return latch;
	}
	public void setLatch(CountDownLatch latch) {
		this.latch = latch;
	}

}


//test.java
package com.cc;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
public class test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		CountDownLatch latch = new CountDownLatch(10);
		ExecutorService execServ = Executors.newFixedThreadPool(10);
		for (int i = 0; i < 10; i++) {
			execServ.submit(new Runner(latch, i));
		}
		try {
			Thread.sleep(1);
			latch.await();
			System.out.println("latch await through pass");
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("--end--");
	}

}

  

//Runner.java
package com.cc;
import java.util.concurrent.CountDownLatch;

public class Runner implements Runnable{
	private CountDownLatch latch;
	private CountDownLatch backLatch;
	private int index;

	public Runner(int index){
		this.index = index;
	}
	
	public Runner(CountDownLatch latch, CountDownLatch backLatch, int index){
		this.latch = latch;
		this.backLatch = backLatch;
		this.index = index;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			System.out.println("latch countDown :"+this.index);
			latch.countDown();
			System.out.println("latch:"+this.index);
			backLatch.await();
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		System.out.println("latch countDown:"+this.index+"end");
	}
	public CountDownLatch getLatch() {
		return latch;
	}
	public void setLatch(CountDownLatch latch) {
		this.latch = latch;
	}

	public CountDownLatch getBackLatch() {
		return backLatch;
	}

	public void setBackLatch(CountDownLatch backLatch) {
		this.backLatch = backLatch;
	}

}

//Test.java
package com.cc;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		CountDownLatch latch = new CountDownLatch(10);
		CountDownLatch backLatch = new CountDownLatch(1);
		ExecutorService execServ = Executors.newFixedThreadPool(10);
		for (int i = 0; i < 10; i++) {
			execServ.submit(new Runner(latch, backLatch,i));
		}
		try {
			latch.await();
			System.out.println("latch await through pass");
			backLatch.countDown();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("--end--");
	}

}

  

 

转载于:https://www.cnblogs.com/fengfengtk/p/5978174.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值