[Feature] add for new
This commit is contained in:
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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<Integer> queue;
|
||||||
|
public Producer(BlockingDeque<Integer> 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<Integer> queue;
|
||||||
|
public Consumer(BlockingDeque<Integer> 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<Integer> queue = new LinkedBlockingDeque<>(100);
|
||||||
|
new Thread(new Producer(queue),"Producer").start();
|
||||||
|
new Thread(new Consumer(queue),"Consumer").start();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user