江的福的小本本之CountDownLatch,CyclicBarrire,Semaphore

CountDownLatch

顾名思义,倒计时发射。看API中介绍它的这一段话。

在这里插入图片描述
一个数字初始化它,当这个数字变为0,await解除。”我main是大哥,绝不会丢下任何一个小弟,你们先走我殿后“
小栗子🌰:

import java.util.concurrent.CountDownLatch;

public class CountDownLatchDemo {


    public static void main(String[] args) {
        CountDownLatch countDownLatch =new CountDownLatch(5);
        for (int i =0 ;i<5 ;i++){
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"要走了");
                countDownLatch.countDown();
            }, String.valueOf(i)).start();
        }
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"我最后走");
    }


}
//使用countdownlatch之后的结果
1要走了
3要走了
4要走了
2要走了
0要走了
main我最后走
//不使用countdownlatch之后的结果
0要走了
4要走了
main我最后走
2要走了
1要走了
3要走了

在这里插入图片描述
插一句题外话:枚举在企业中的小应用。话不多说,上代码:


public class CountDownLatchDemo {

    enum mysql{
        One(1,"百度"),TWO(2,"阿里"),Three(3,"腾讯"),Four(4,"华为"),Five(5,"硅谷");

        private Integer num ;
        private String name;

        public String getName() {
            return name;
        }
         mysql(Integer num,String name) {
            this.name=name;
            this.num=num;
        }
        public static mysql forech_enum(int index) {
            mysql[] mysqls = CountDownLatchDemo.mysql.values();
            for (mysql element:mysqls){
                if (element.num==index){
                    return element;
                }
            }
            return  null;
        }

    }

    public static void main(String[] args) {
        CountDownLatch countDownLatch =new CountDownLatch(5);
        for (int i =1 ;i<=5 ;i++){
            new Thread(()->{
                System.out.println("请先生接收来自-"+Thread.currentThread().getName()+"-的offer");
                countDownLatch.countDown();
            }, mysql.forech_enum(i).getName()).start();
        }
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("棒呆!!!");
    }


}

结果:

请先生接收来自-百度-的offer
请先生接收来自-腾讯-的offer
请先生接收来自-阿里-的offer
请先生接收来自-硅谷-的offer
请先生接收来自-华为-的offer
棒呆!!!

这是为了避免在工作中,因为有时要修改数据,而实现的一个小型数据库。

CyclicBarrire

与CountDownLatch相反,”集齐七颗龙珠,才能召唤神龙“ 。

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

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 i1 =i;
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"拿到了第"+i1+"颗龙珠");
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            },String.valueOf(i) ).start();
        }
    }

}

结果:

2拿到了第2颗龙珠
6拿到了第6颗龙珠
5拿到了第5颗龙珠
4拿到了第4颗龙珠
7拿到了第7颗龙珠
1拿到了第1颗龙珠
3拿到了第3颗龙珠
已经集齐七颗龙珠,召唤神龙
Semaphore

类似抢车位,多资源抢占多资源。

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

public class SemaphoreDemo {
    public static void main(String[] args) {
        Semaphore semaphore =new Semaphore(3);
        for (int i =1;i<=6;i++){
            new Thread(()->{
                try {
                    semaphore.acquire();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+" 抢上车位");
                try {
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("待了三秒走了");
                semaphore.release();
            },String.valueOf(i) ).start();
        }
    }
}

结果

1 抢上车位
2 抢上车位
3 抢上车位
待了三秒走了
4 抢上车位
待了三秒走了
待了三秒走了
5 抢上车位
6 抢上车位
待了三秒走了
待了三秒走了
待了三秒走了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值