CountDownLatch CyclicBarrier Semaphore简单介绍

CountDownLatch
让一些线程阻塞直到另一些线程完成一系列操作后才被唤醒
之前在ZookeeperJavaAPI基本操作使用到了它
CountDownLatch主要有两种方法,当一个或者多个线程调用await方法时,调用线程会被阻塞,其他线程调用countDown方法会将计数器减1(调用countDown方法不会阻塞)
当计数器的值变成零时,因调用awaitt方法被阻塞的线程会被唤醒,继续执行

import java.util.concurrent.CountDownLatch;

public class CountDownLatchDemo {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(6);
        for (int i = 0; i <6 ; i++) {
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"\t 上课结束。离开教室!");
                countDownLatch.countDown();
            },String.valueOf(i)).start();
        }
        countDownLatch.await();
        System.out.println("============最后关灯锁门===========");
    }
}
/*
0   上课结束。离开教室!
2   上课结束。离开教室!
1   上课结束。离开教室!
3   上课结束。离开教室!
4   上课结束。离开教室!
5   上课结束。离开教室!
============最后关灯锁门===========

* */

public enum  CountryEnum {
    One(1,"齐"),TWO(2,"楚"),THREE(3,"燕"),FOUR(4,"赵"),FIVE(5,"魏"),SIX(6,"赵");

     private Integer retCode;
     private  String retMessage1;
     private  String retMessage2;

    public Integer getRetCode() {
        return retCode;
    }

    public String getRetMessage() {
        return retMessage1;
    }

    public String getRetMessage2() {
        return retMessage2;
    }

    CountryEnum(Integer retCode, String retMessage1) {
        this.retCode = retCode;
        this.retMessage1 = retMessage1;
    }
    public static CountryEnum foreach_enum(int index){
        CountryEnum[] enums = CountryEnum.values();
        for (CountryEnum element:enums){
            if (index ==element.retCode){
                return  element;
            }

        }
        return null;
    }
}



    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(6);
        for (int i = 1; i <7 ; i++) {
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"\t 国,被灭!");
                countDownLatch.countDown();
            },CountryEnum.foreach_enum(i).getRetMessage()).start();
        }
        countDownLatch.await();
        System.out.println("============秦国统一天下===========");
        System.out.println(CountryEnum.SIX);
        System.out.println(CountryEnum.SIX.getRetCode());
    }
}
/*
齐   国,被灭!
楚   国,被灭!
燕   国,被灭!
赵   国,被灭!
魏   国,被灭!
赵   国,被灭!
============秦国统一天下===========
SIX
6
* */

CyclicBarrier
顾名思义,就是可循环(Cylic)使用的屏障(Barrier)
主要是让一组线程到达一个屏障(同步点)时被阻塞,直到最后一个线程到达平展时,屏障会被打开,所有被屏障拦截的线程才会继续运行,线程进入屏障通过CyclicBarrier的await方法

public class CyclicBarrierDemo {
    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(7,()->{
            System.out.println("***********召唤神龙!**************");
        });
        for (int i = 1; i <=7 ; i++) {
            final int tempInt = i;
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"\t 收集第:"+tempInt+"龙珠");
                try {
                   cyclicBarrier.await();//得到龙珠的线程等待
                } catch (Exception e) {
                    e.printStackTrace();
                }
            },String.valueOf(i)).start();

        }
    }
}
/*
1   收集第:1龙珠
2   收集第:2龙珠
4   收集第:4龙珠
3   收集第:3龙珠
5   收集第:5龙珠
6   收集第:6龙珠
7   收集第:7龙珠
***********召唤神龙!**************
* */

Semphone
Semphone主要用于多个共享资源的互斥使用,以及并发线程数的控制


public class SemaphoreDemo {
    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(3);//3个停车位
        for (int i = 1; i <=6 ; i++) {
            new Thread(()->{
                try {
                   semaphore.acquire();
                    System.out.println(Thread.currentThread().getName()+"\t 抢到车位");
                    TimeUnit.SECONDS.sleep(3);
                    System.out.println(Thread.currentThread().getName()+"\t 离开车位");
                } catch (Exception e) {
                    e.printStackTrace();
                }finally {
                semaphore.release();
                }
            },String.valueOf(i)).start();
        }
    }
}
/*
2   抢到车位
3   抢到车位
1   抢到车位
3   离开车位
1   离开车位
2   离开车位
5   抢到车位
4   抢到车位
6   抢到车位
5   离开车位
4   离开车位
6   离开车位
* */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值