From d06effa30659d248b6d08fbb548f59d5a28c3acb Mon Sep 17 00:00:00 2001 From: "binbin.hou" Date: Sun, 8 Nov 2020 13:07:13 +0800 Subject: [PATCH] [Feature] add for new --- .../com/github/houbb/lock/test/MyPhaser.java | 25 ++++++++ .../github/houbb/lock/test/PhaserDemo.java | 53 +++++++++++++++++ .../test/lock/LinkedBlockingDequeDemo.java | 57 +++++++++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 lock-test/src/test/java/com/github/houbb/lock/test/MyPhaser.java create mode 100644 lock-test/src/test/java/com/github/houbb/lock/test/PhaserDemo.java create mode 100644 lock-test/src/test/java/com/github/houbb/lock/test/lock/LinkedBlockingDequeDemo.java diff --git a/lock-test/src/test/java/com/github/houbb/lock/test/MyPhaser.java b/lock-test/src/test/java/com/github/houbb/lock/test/MyPhaser.java new file mode 100644 index 0000000..6a02c7d --- /dev/null +++ b/lock-test/src/test/java/com/github/houbb/lock/test/MyPhaser.java @@ -0,0 +1,25 @@ +package com.github.houbb.lock.test; + +import java.util.concurrent.Phaser; + +/** + * @author binbin.hou + * @since 1.0.0 + */ +public class MyPhaser extends Phaser { + + @Override + protected boolean onAdvance(int phase, int registeredParties) { + switch (phase) { + case 0 : + System.out.println("上半场完成"); + return false; + case 1: + System.out.println("下半场完成"); + return false; + default: + return true; + } + } + +} diff --git a/lock-test/src/test/java/com/github/houbb/lock/test/PhaserDemo.java b/lock-test/src/test/java/com/github/houbb/lock/test/PhaserDemo.java new file mode 100644 index 0000000..42e573a --- /dev/null +++ b/lock-test/src/test/java/com/github/houbb/lock/test/PhaserDemo.java @@ -0,0 +1,53 @@ +package com.github.houbb.lock.test; + +import java.util.concurrent.Phaser; + +/** + * @author binbin.hou + * @since 1.0.0 + */ +public class PhaserDemo { + + private static class GameRunnable implements Runnable { + + private final Phaser phaser; + + private GameRunnable(Phaser phaser) { + this.phaser = phaser; + } + + @Override + public void run() { + //参加上半场比赛 + System.out.println("玩家-"+Thread.currentThread().getName()+":参加上半场比赛"); + //执行这个方法的话会等所有的选手都完成了之后再继续下面的方法 + phaser.arriveAndAwaitAdvance(); + + + // 下半场 + //参加上半场比赛 + System.out.println("玩家-"+Thread.currentThread().getName()+":参加下半场比赛"); + //执行这个方法的话会等所有的选手都完成了之后再继续下面的方法 + phaser.arriveAndAwaitAdvance(); + } + } + + public static void main(String[] args) { + int nums = 3; + Phaser phaser = new MyPhaser(); + + //注册一次表示 phaser 维护的线程个数 + phaser.register(); + + for(int i = 0; i < nums; i++) { + phaser.register(); + + Thread thread = new Thread(new GameRunnable(phaser)); + thread.start(); + } + + //后续阶段主线程就不参加了 + phaser.arriveAndDeregister(); + } + +} diff --git a/lock-test/src/test/java/com/github/houbb/lock/test/lock/LinkedBlockingDequeDemo.java b/lock-test/src/test/java/com/github/houbb/lock/test/lock/LinkedBlockingDequeDemo.java new file mode 100644 index 0000000..d7c3845 --- /dev/null +++ b/lock-test/src/test/java/com/github/houbb/lock/test/lock/LinkedBlockingDequeDemo.java @@ -0,0 +1,57 @@ +package com.github.houbb.lock.test.lock; + +import java.util.concurrent.BlockingDeque; +import java.util.concurrent.LinkedBlockingDeque; +import java.util.concurrent.ThreadLocalRandom; + +/** + * @author binbin.hou + * @since 1.0.0 + */ +public class LinkedBlockingDequeDemo { + + private static class Producer implements Runnable{ + private BlockingDeque queue; + public Producer(BlockingDeque queue) { + this.queue = queue; + } + @Override + public void run() { + while(true) { + try { + Integer num = ThreadLocalRandom.current().nextInt(100); + queue.put(num); + System.out.println(String.format("%s producer a num %d",Thread.currentThread().getName(),num)); + Thread.sleep(1000); + } catch (InterruptedException e1) { + e1.printStackTrace(); + } + } + } + } + + private static class Consumer implements Runnable{ + private BlockingDeque queue; + public Consumer(BlockingDeque queue) { + this.queue = queue; + } + @Override + public void run() { + while(true) { + try { + System.out.println(String.format("%s consume a num %d",Thread.currentThread().getName(),queue.take())); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + } + + public static void main(String[] args) { + BlockingDeque queue = new LinkedBlockingDeque<>(100); + new Thread(new Producer(queue),"Producer").start(); + new Thread(new Consumer(queue),"Consumer").start(); + } + + +}