欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

CountDownLatch 博客分类: concurrent concurrent 

程序员文章站 2024-03-06 19:45:56
...
一、CountDownLatch

一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待。

用给定的计数 初始化 CountDownLatch。由于调用了 countDown() 方法,所以在当前计数到达零之前,await 方法会一直受阻塞。之后,会释放所有等待的线程,await 的所有后续调用都将立即返回。这种现象只出现一次——计数无法被重置。如果需要重置计数,请考虑使用 CyclicBarrier。




import java.util.concurrent.CountDownLatch;

/**
 * 某一个事情按照指定的步骤进行
 */
public class CountDownLatchDemo {

	/**
	 * @description
	 * 去上班之前
	 * 1.洗脸
	 * 2.刷牙
	 * 3.吃饭
	 * @param args
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws InterruptedException {

		// 指明时间的数量
		CountDownLatch latch = new CountDownLatch(3);
		
		new Thread(new WashFace(latch)).start();
		new Thread(new WashTooth(latch)).start();
		new Thread(new Eat(latch)).start();
		// 阻塞线程,直到 latch 的值为 0 
		latch.await();
		// 若不适用 CountDownLatch 则会出现,主线程信息先打印,而等待其他线程信息的情况
		System.out.println("去上班");
	}

}

class WashFace implements Runnable{
	
	private CountDownLatch latch = null ;
	
	public WashFace(){
		
	}
	public WashFace(CountDownLatch latch){
		this.latch = latch ;
	}
	
	@Override
	public void run() {
		System.out.println("洗脸");
		latch.countDown();
	}
}

class WashTooth implements Runnable{

	private CountDownLatch latch = null ;
	
	public WashTooth(){
		
	}	
	
	public WashTooth(CountDownLatch latch){
		this.latch = latch ;
	}
	
	@Override
	public void run() {

		System.out.println("刷牙");
		latch.countDown();
	}
	
}

class Eat implements Runnable{

	private CountDownLatch latch ;
	
	public Eat(){
		
	}
	
	public Eat(CountDownLatch latch){
		this.latch = latch ;
	}
	
	@Override
	public void run() {
		System.out.println("吃饭");
		latch.countDown();
	}
	
}


二、

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * 需求:某一个时间点要求各个线程统一开始执行
 * 先执行到该时间点的线程等待其他线程;然后所有线程一起运行
 */
public class CyclicBarrierDownLatchDemo {

	/**
	 * @description
	 * 赛马
	 * 1.比赛开始前,所有马匹被从其他地方牵引到起点位置
	 * 2.先到的等待后到的
	 * 3.比赛开始,所有马匹开始赛跑
	 * @param args
	 */
	public static void main(String[] args) {

		CyclicBarrier cb = new CyclicBarrier(3);
		new Thread(new BlackHorse(cb)).start();
		new Thread(new WhiteHorse(cb)).start();
		new Thread(new GreyHorse(cb)).start();
		
		System.out.println("=============");
		
	}

}

class BlackHorse implements Runnable{

	private CyclicBarrier cb = null ; 
	public BlackHorse(){
		
	}
	public BlackHorse(CyclicBarrier cb){
		this.cb = cb ;
	}
	
	@Override
	public void run() {
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("黑马花费1S到达起点");
		try {
			cb.await();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (BrokenBarrierException e) {
			e.printStackTrace();
		}
		
		System.out.println("黑马开始奔跑");
	}
	
}

class WhiteHorse implements Runnable{

	private CyclicBarrier cb = null ; 
	public WhiteHorse(){
		
	}
	public WhiteHorse(CyclicBarrier cb){
		this.cb = cb ;
	}
	
	@Override
	public void run() {
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("白马花费2S到达起点");
		try {
			cb.await();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (BrokenBarrierException e) {
			e.printStackTrace();
		}
		
		System.out.println("白马开始奔跑");
	}
	
}

class GreyHorse implements Runnable{

	private CyclicBarrier cb = null ; 
	public GreyHorse(){
		
	}
	public GreyHorse(CyclicBarrier cb){
		this.cb = cb ;
	}
	
	@Override
	public void run() {
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("灰马花费3S到达起点");
		try {
			cb.await();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (BrokenBarrierException e) {
			e.printStackTrace();
		}
		
		System.out.println("灰马开始奔跑");
	}
	
}




三、Exchanger


import java.util.concurrent.Exchanger;

/**
 * 线程间交换信息,允许线程间相互等待 
 */
public class ExchangerDemo {

	/**
	 * @description
	 * 1.A来到指定地点等待B,发出信息
	 * 2.B来到指定地点等待A,发出信息
	 * @param args
	 */
	public static void main(String[] args) {

		Exchanger<String> ex = new Exchanger<String>();
		
		new Thread(new PersonA(ex)).start();
		new Thread(new PersonB(ex)).start();
		
		System.out.println("============");
	}

}
class PersonB implements Runnable{

	private Exchanger<String> ex ;
	public PersonB(Exchanger<String> ex){
		this.ex = ex ;
	}
	
	@Override
	public void run() {
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("B用5S到达了指点地点");
		try {
			String result = ex.exchange("宝塔镇河妖");
			if(result.equals("天王盖地虎")){
				System.out.println("自己人");
			}else{
				System.out.println("兄弟们,抄家伙儿");
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
}
class PersonA implements Runnable{

	private Exchanger<String> ex ;
	public PersonA(Exchanger<String> ex){
		this.ex = ex ;
	}
	
	@Override
	public void run() {
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("A用1S到达了指点地点");
		try {
			String result = ex.exchange("天王盖地虎");
			if(result.equals("宝塔镇河妖")){
				System.out.println("自己人");
			}else{
				System.out.println("兄弟们,抄家伙儿");
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
}



四、Semaphore

1.

import java.util.concurrent.Semaphore;

/**
 * 保护一段代码,不能同时被超过N个线程访问
 */
public class SemaphoreDemo {

	/**
	 * @description
	 * 景区某一个景点,每次仅允许3人进入访问;一旦有人出来,再次放人进入
	 * @param args
	 */
	public static void main(String[] args) {

		Semaphore se = new Semaphore(3);
                // 保证公平,按照顺序执行
		// Semaphore se = new Semaphore(3,true);
		for(int i = 0 ; i < 100 ; i++){
			new Thread(new Visitor(se, i)).start();
		}
	}
}

class Visitor implements Runnable{

	private Semaphore se = null ;
	private int i ;
	public Visitor(){
		
	}
	
	public Visitor(Semaphore se,int i){
		this.se = se ;
		this.i = i ;
	}
	
	@Override
	public void run() {

		try {
			se.acquire();
			System.out.println(i+"进入景区");
			Thread.sleep(i*1000);
			System.out.println(i+"游览"+i*1000+"s");
			se.release();
			System.out.println(i+"离开景区");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
}


运行结果:
0进入景区
4进入景区
2进入景区
0游览0s
...

未按照顺序进行执行
相关标签: concurrent