概念 demo1 import java . util . Map ; import java . util . concurrent . ConcurrentHashMap ; import java . util . concurrent . CyclicBarrier ; /** * 类说明:演示CyclicBarrier用法,共4个子线程,他们全部完成工作后,交
概念
demo1
import java.util.Map;import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CyclicBarrier;
/**
* 类说明:演示CyclicBarrier用法,共4个子线程,他们全部完成工作后,交出自己结果,
* 再被统一释放去做自己的事情,而交出的结果被另外的线程拿来拼接字符串
* <p>
案例:ZJJ_JavaBasic_2020/10/19_19:34:13_vg4uh
*/
public class demo1 {
/**
* 第一个参数必须和要互相协调的线程数一样,必须一样,如果多的话,会使劲等,无法继续执行
*/
private static CyclicBarrier barrier
= new CyclicBarrier(4, new CollectThread());
//存放子线程工作结果的容器
private static ConcurrentHashMap<String, Long> resultMap
= new ConcurrentHashMap<>();
public static void main(String[] args) {
for (int i = 0; i < 4; i++) {
Thread thread = new Thread(new SubThread());
thread.start();
}
}
/*汇总的任务*/
private static class CollectThread implements Runnable {
public void run() {
StringBuilder result = new StringBuilder();
/*把多个线程的结果汇总一下*/
for (Map.Entry<String, Long> workResult : resultMap.entrySet()) {
result.append("[" + workResult.getValue() + "]");
}
System.out.println(" 结果是 = " + result);
System.out.println("做其它的 business........");
}
}
/*相互等待的子线程*/
private static class SubThread implements Runnable {
public void run() {
long id = Thread.currentThread().getId();
resultMap.put(Thread.currentThread().getId() + "", id);
try {
Thread.sleep(1000 + id);
System.out.println("Thread_" + id + " ....do something ");//四个线程会一起做这个
/**
* await()方法可以反复调用, 调用完了CyclicBarrier可以反复重复的去触发
*/
barrier.await();
Thread.sleep(1000 + id);
System.out.println("Thread_" + id + " ....do its business ");
/**
* await()方法可以反复调用, 调用完了CyclicBarrier可以反复重复的去触发
*
*/
barrier.await();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
控制台打印结果:
Thread_12 ....do somethingThread_13 ....do something
Thread_14 ....do something
Thread_15 ....do something
结果是 = [12][13][14][15]
做其它的 business........
Thread_12 ....do its business
Thread_13 ....do its business
Thread_15 ....do its business
Thread_14 ....do its business
结果是 = [12][13][14][15]
做其它的 business........
demo2
import java.util.Map;import java.util.concurrent.*;
/**
* 演示主线程等待四个子线程统计完了,主线程再获取四个子线程的结果.
* <p>
* 案例:ZJJ_JavaBasic_2020/10/19_19:34:13_vg4uh
*/
public class demo2 implements Runnable {
/*创建四个屏障,处理完之后执行当前类的run方法*/
private CyclicBarrier c = new CyclicBarrier(4, this);
/*假设只有4个sheet,所以只启动4个线程*/
private Executor executor = Executors.newFixedThreadPool(4);
/*保存每个sheet计算出的银流结果*/
private ConcurrentHashMap<String, Integer> sheetBankWaterCount = new
ConcurrentHashMap<String, Integer>();
protected void count() {
for (int i = 0; i < 4; i++) {
executor.execute(new Runnable() {
public void run() {
sheetBankWaterCount.put(Thread.currentThread().getName(), 1);
try {
int await = c.await();
} catch (InterruptedException | BrokenBarrierException e) {
e.printStackTrace();
}
}
});
}
}
public void run() {
int result = 0;
// 汇总每个sheet计算出的结果
for (Map.Entry<String, Integer> sheet : sheetBankWaterCount.entrySet()) {
result += sheet.getValue();
}
// 将结果输出
sheetBankWaterCount.put("result", result);
System.out.println(result);
}
public static void main(String[] args) {
demo2 bankWaterService = new demo2();
bankWaterService.count(); //结果是 4 因为四个线程统计,每个线程假设为1 所以结果就是4
}
}
控制台打印结果
Thread_12 ....do somethingThread_13 ....do something
Thread_14 ....do something
Thread_15 ....do something
结果是 = [12][13][14][15]
做其它的 business........
Thread_12 ....do its business
Thread_13 ....do its business
Thread_14 ....do its business
Thread_15 ....do its business
结果是 = [12][13][14][15]
做其它的 business........