CountDownLatch && CyclicBarrier example

CountDownLatch && CyclicBarrier example

CountDownLatch

package com.lbd.concurrent;
import java.util.Random;
import java.util.concurrent.*;
/**
* Created by lbd.
*/
public class CountDownLatchDemo {
public static void main(String[] args) {
ExecutorService executor = Executors.newCachedThreadPool();
CountDownLatch latch = new CountDownLatch(3);
Worker w1 = new Worker("worker1", latch);
Worker w2 = new Worker("worker2", latch);
Worker w3 = new Worker("worker3", latch);
Boss boss = new Boss(latch);
executor.execute(w1);
executor.execute(w2);
executor.execute(w3);
executor.execute(boss);
executor.shutdown();
}
}
class Worker implements Runnable {
private CountDownLatch downLatch;
private String name;
public Worker(String name, CountDownLatch downLatch) {
this.name = name;
this.downLatch = downLatch;
}
public void run() {
System.out.println(this.name + " is working...");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(this.name + " is done!");
downLatch.countDown();
}
}
class Boss implements Runnable {
private CountDownLatch downLatch;
public Boss(CountDownLatch downLatch) {
this.downLatch = downLatch;
}
public void run() {
System.out.println("boss is waiting for all workers...");
try {
downLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("all work is done! Boss gonna check");
}
}

output:

worker1 is working...
worker2 is working...
worker3 is working...
boss is waiting for all workers...
worker2 is done!
worker3 is done!
worker1 is done!
all work is done! Boss gonna check

CyclicBarrier

package com.lbd.concurrent;
import java.util.Random;
import java.util.concurrent.*;
/**
* Created by lbd.
*/
public class CyclicBarrierDemo {
public static void main(String[] args) {
ExecutorService executor = Executors.newCachedThreadPool();
CyclicBarrier barrier = new CyclicBarrier(3);
Worker1 w1 = new Worker1("worker1", barrier);
Worker1 w2 = new Worker1("worker2", barrier);
Worker1 w3 = new Worker1("worker3", barrier);
executor.execute(w1);
executor.execute(w2);
executor.execute(w3);
executor.shutdown();
}
}
class Worker1 implements Runnable {
private CyclicBarrier barrier;
private String name;
public Worker1(String name, CyclicBarrier barrier) {
this.barrier = barrier;
this.name = name;
}
@Override
public void run() {
try {
Thread.sleep(new Random().nextInt(10000));
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(barrier.getNumberWaiting() + " worker arrived");
System.out.println(this.name + " arrived");
try {
barrier.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
System.out.println("all arrived,star working!");
}
}

output:

0 worker arrived
worker2 arrived
1 worker arrived
worker1 arrived
2 worker arrived
worker3 arrived
all arrived,star working!
all arrived,star working!
all arrived,star working!

欢迎关注公众号: FullStackPlan 获取更多干货

Copyright © 2016 - 2017 LBD's Blog All Rights Reserved.

访客数 : | 访问量 :