CountDownLatch、CyclicBarrier、Semaphore

 1 package com.atguigu.boot.com.atguigu;
 2 
 3 import com.sun.org.apache.bcel.internal.generic.NEW;
 4 
 5 import java.util.concurrent.CountDownLatch;
 6 
 7 public class CountDownLatchDemo {
 8     private static final int CONSTANT_NUM=6;
 9 
10     public static void main(String[] args) throws InterruptedException {
11         CountDownLatch count=new CountDownLatch(CONSTANT_NUM);
12         for (int i = 1; i <7 ; i++) {
13             new Thread(()->{
14                 System.out.println(Thread.currentThread().getName()+"\t 国,被灭");
15                 count.countDown();
16             },CountryEnum.forEach_CountryEnum(i).getMess()).start();
17         }
18         count.await();
19         System.out.println(Thread.currentThread().getName()+"\t ********秦帝国,统一华夏");
20     }
21 
22     private static void closeDoor() throws InterruptedException {
23         CountDownLatch count=new CountDownLatch(CONSTANT_NUM);
24         for (int i = 0; i <6 ; i++) {
25             new Thread(()->{
26                 System.out.println(Thread.currentThread().getName()+"\t 上完自习,离开教室");
27                 count.countDown();
28             },String.valueOf(i)).start();
29         }
30         count.await();
31         System.out.println(Thread.currentThread().getName()+"\t ********班长最后关门走人");
32     }
33 }
package com.atguigu.boot.com.atguigu;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
//循环栅栏,只有线程到达指定的数量才能够往下执行
public class CyclicBarrierDemo {

    public static void main(String[] args) {
        CyclicBarrier cb=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 {
                    cb.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            },String.valueOf(i)).start();
        }
    }
}
package com.atguigu.boot.com.atguigu;

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

public class SemaphoreDemo {

    public static void main(String[] args) {
        Semaphore semaphore=new Semaphore(3,false);//false非公平锁

        for (int i = 0; i < 6; i++) {
            new Thread(()->{
                try {
                    semaphore.acquire();
                    System.out.println(Thread.currentThread().getName()+"\t 抢到车位");
                    try {TimeUnit.SECONDS.sleep(3);} catch (InterruptedException e) {e.printStackTrace();}
                    System.out.println(Thread.currentThread().getName()+"\t 停车3秒后离开车位");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    semaphore.release();
                }
            },String.valueOf(i)).start();
        }
    }
}

 

posted @ 2021-05-04 22:55  ffzzblog  阅读(47)  评论(0)    收藏  举报