Java????????????????
???????????? ???????[ 2016/12/5 11:14:30 ] ????????????? JAVA
??????????????JDK??????????????????
????1??CountDownLatch????????????????????????????????裩????
????2??CyclicBarrier??????????????????
????3??Semaphore(????????????????????)
????????ο??????java??????????????Java????——?????????
????????CountDownLatch?????????????????–??????????????
????1.????????????????????????????????????
????CountDownLatch??????????????int??????????????????????????N?????????????????N.
????2.countDown???????????????CountDownLatch??countDown???????N???1??CountdownLatch??await????????????????????N?????
????3.????????????CountDown?????????κε???????????????N??????????N??????????????N?????衣
????4.await????????????countDown?????????????????????????????????????????????await????????????????????????????????????????????????????????await???????????г???????await??????????????????????????????????????????????????????
????5.????API
????1????????
????CountDownLatch(int count)?? ?????????????????????????
????2??void await???????????????????????????????????????????????????ж??
????3??boolean await??long timeout??TimeUnit timeUnit?????????????????????????????????????????????????ж??????????????????????
????4??void countDown() ??????1?????????0??????????е????????
????5) long getCount() ????????????
????6) String toString() ???????????????????????????
???????????СС????
?????????????????????????????????????????????????????????
????package multiTask.bean.countDownLatch;
????import java.util.concurrent.*;
????/**
????* Created by zyl on 2016/12/4.
????*/
????public class CountDownLatchDemo {
????public static void main(String[] arg) throws InterruptedException {
????System.out.println("current thread ..." + Thread.currentThread().getName());
????CountDownLatch count = new CountDownLatch(3);
????ExecutorService executorService = Executors.newCachedThreadPool();
????executorService.execute(new Worker(count?? "???"));
????executorService.execute(new Worker(count?? "???"));
????executorService.execute(new Worker(count?? "??"));
????new Boss(count?? "???").awaitForWorkerDone();
????System.out.println("???????鶼????????????????");
????executorService.shutdown();
????}
????static class Boss {
????private CountDownLatch count;
????private String bossName;
????public Boss(CountDownLatch count?? String bossName) {
????this.bossName = bossName;
????this.count = count;
????}
????public void awaitForWorkerDone() throws InterruptedException {
????this.count.await();
????System.out.println("Boss monitor is done.");
????}
????}
????static class Worker implements Runnable {
????private CountDownLatch count;
????private String workerName;
????public Worker(CountDownLatch count?? String workerName) {
????this.count = count;
????this.workerName = workerName;
????}
????@Override
????public void run() {
????System.out.println(workerName + " " + Thread.currentThread().getName() + " is running ..");
????try {
????Thread.sleep(3000);
????} catch (InterruptedException e) {
????e.printStackTrace();
????}
????/*
????* ????????????????1*
????* */
????this.count.countDown();
????}
????}
????}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11