目录
-
- 一、CountDownLatch
- 二、Semaohore
- 三、Cyclicbarrier
- 四、总结
一、CountDownLatch
package flowcontrol.countdownlatch;import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;/*** 描述: 工厂中,质检,5个工人检查,所有人都认为通过,才通过*/
public class CountDownLatchDemo1 {
public static void main(String[] args) throws InterruptedException {
CountDownLatch latch = new CountDownLatch(5);ExecutorService service = Executors.newFixedThreadPool(5);for (int i = 0; i < 5; i++) {
final int no = i + 1;Runnable runnable = new Runnable() {
@Overridepublic void run() {
try {
Thread.sleep((long) (Math.random() * 10000));System.out.println("No." + no + "完成了检查。");} catch (InterruptedException e) {
e.printStackTrace();} finally {
latch.countDown();}}};service.submit(runnable);}System.out.println("等待5个人检查完.....");latch.await();System.out.println("所有人都完成了工作,进入下一个环节。");}
}
package flowcontrol.countdownlatch;import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;/*** 描述: 模拟100米跑步,5名选手都准备好了,只等裁判员一声令下,所有人同时开始跑步。*/
public class CountDownLatchDemo2 {
public static void main(String[] args) throws InterruptedException {
CountDownLatch begin = new CountDownLatch(1);ExecutorService service = Executors.newFixedThreadPool(5);for (int i = 0; i < 5; i++) {
final int no = i + 1;Runnable runnable = new Runnable() {
@Overridepublic void run() {
System.out.println("No." + no + "准备完毕,等待发令枪");try {
begin.await();System.out.println("No." + no + "开始跑步了");} catch (InterruptedException e) {
e.printStackTrace();}}};service.submit(runnable);}//裁判员检查发令枪...Thread.sleep(5000);System.out.println("发令枪响,比赛开始!");begin.countDown();}
}
运行结果
package flowcontrol.countdownlatch;import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;/*** 描述: 模拟100米跑步,5名选手都准备好了,只等裁判员一声令下,所有人同时开始跑步。当所有人都到终点后,比赛结束。*/
public class CountDownLatchDemo1And2 {
public static void main(String[] args) throws InterruptedException {
CountDownLatch begin = new CountDownLatch(1);CountDownLatch end = new CountDownLatch(5);ExecutorService service = Executors.newFixedThreadPool(5);for (int i = 0; i < 5; i++) {
final int no = i + 1;Runnable runnable = new Runnable() {
@Overridepublic void run() {
System.out.println("No." + no + "准备完毕,等待发令枪");try {
begin.await();System.out.println("No." + no + "开始跑步了");Thread.sleep((long) (Math.random() * 10000));System.out.println("No." + no + "跑到终点了");} catch (InterruptedException e) {
e.printStackTrace();} finally {
end.countDown();}}};service.submit(runnable);}//裁判员检查发令枪...Thread.sleep(5000);System.out.println("发令枪响,比赛开始!");begin.countDown();end.await();System.out.println("所有人到达终点,比赛结束");}
}
二、Semaohore
package flowcontrol.semaphore;import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;/*** 描述: 演示Semaphore用法*/
public class SemaphoreDemo {
static Semaphore semaphore = new Semaphore(5, true);public static void main(String[] args) {
ExecutorService service = Executors.newFixedThreadPool(50);for (int i = 0; i < 100; i++) {
service.submit(new Task());}service.shutdown();}static class Task implements Runnable {
@Overridepublic void run() {
try {
semaphore.acquire(3);} catch (InterruptedException e) {
e.printStackTrace();}System.out.println(Thread.currentThread().getName() + "拿到了许可证");try {
Thread.sleep(2000);} catch (InterruptedException e) {
e.printStackTrace();}System.out.println(Thread.currentThread().getName() + "释放了许可证");semaphore.release(2);}}
}
三、Cyclicbarrier
package flowcontrol.cyclicbarrier;import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;/*** 描述: 演示CyclicBarrier*/
public class CyclicBarrierDemo {
public static void main(String[] args) {
CyclicBarrier cyclicBarrier = new CyclicBarrier(5, new Runnable() {
@Overridepublic void run() {
System.out.println("所有人都到场了, 大家统一出发!");}});for (int i = 0; i < 10; i++) {
new Thread(new Task(i, cyclicBarrier)).start();}}static class Task implements Runnable{
private int id;private CyclicBarrier cyclicBarrier;public Task(int id, CyclicBarrier cyclicBarrier) {
this.id = id;this.cyclicBarrier = cyclicBarrier;}@Overridepublic void run() {
System.out.println("线程" + id + "现在前往集合地点");try {
Thread.sleep((long) (Math.random()*10000));System.out.println("线程"+id+"到了集合地点,开始等待其他人到达");cyclicBarrier.await();System.out.println("线程"+id+"出发了");} catch (InterruptedException e) {
e.printStackTrace();} catch (BrokenBarrierException e) {
e.printStackTrace();}}}}