当前位置: 代码迷 >> 综合 >> CountDownLatch CyclicBarrier Semaphore简单介绍
  详细解决方案

CountDownLatch CyclicBarrier Semaphore简单介绍

热度:76   发布时间:2023-12-15 10:47:03.0

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 离开车位 * */
  相关解决方案