//Listing 6-5. Using a Phaser to Control a One-Shot Action Serving a Variable Number
//of Parties
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.Phaser; public class A {
public static void main(String[] args)
{
List<Runnable> tasks = new ArrayList<>(); tasks.add(() -> System.out.printf("%s running at %d%n",
Thread.currentThread().getName(),
System.currentTimeMillis())); tasks.add(() -> System.out.printf("%s running at %d%n",
Thread.currentThread().getName(),
System.currentTimeMillis()));
runTasks(tasks);
} static void runTasks(List<Runnable> tasks)
{
final Phaser phaser = new Phaser(); // "1" (register self)
// create and start threads
for (final Runnable task: tasks)
{
phaser.register();
Runnable r = () ->
{
try
{
Thread.sleep( + (int) (Math.random() * ));
}
catch (InterruptedException ie)
{
System.out.println("interrupted thread");
}
phaser.arriveAndAwaitAdvance(); // await the ...
// creation of ...
// all tasks
task.run();
}; Executors.newSingleThreadExecutor().execute(r);
}
// allow threads to start and deregister self
phaser.arriveAndDeregister();
}
}

Java Phaser的更多相关文章

  1. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  2. 【Java并发编程实战】-----“J.U.C”:Phaser

    Phaser由java7中推出,是Java SE 7中新增的一个使用同步工具,在功能上面它与CyclicBarrier.CountDownLatch有些重叠,但是它提供了更加灵活.强大的用法. Cyc ...

  3. Java并发学习之十九——线程同步工具之Phaser

    本文是学习网络上的文章时的总结.感谢大家无私的分享. JDK 1.7 加入了一个新的工具Phaser.Phaser的在功能上与CountDownLatch有部分重合. 以下使用Phaser类来同步3个 ...

  4. Java Concurrency - 浅析 Phaser 的用法

    One of the most complex and powerful functionalities offered by the Java concurrency API is the abil ...

  5. Java Concurrency - Phaser, Controlling phase change in concurrent phased tasks

    The Phaser class provides a method that is executed each time the phaser changes the phase. It's the ...

  6. Java并发之CyclicBarrier、CountDownLatch、Phaser

    在Java多线程编程中,经常会需要我们控制并发流程,等其他线程执行完毕,或者分阶段执行.Java在1.5的juc中引入了CountDownLatch和CyclicBarrier,1.7中又引入了Pha ...

  7. Java并发编程的4个同步辅助类(CountDownLatch、CyclicBarrier、Semaphore、Phaser)

    我在<JDK1.5引入的concurrent包>中,曾经介绍过CountDownLatch.CyclicBarrier两个类,还给出了CountDownLatch的演示案例.这里再系统总结 ...

  8. 【Java并发核心三】CountDownLatch、CyclicBarrier及Phaser

    个人感觉,看书学习还是需要“不求甚解”,因为一旦太过于计较小的得失,就容易钻牛角尖,学习进度也慢.我们完全可以先学一个大概,等到真正用到的时候再把那些细节丰富起来,就更有针对性. 所以,针对java并 ...

  9. Java并发编程的4个同步辅助类(CountDownLatch、CyclicBarrier、Semphore、Phaser)

    我在<jdk1.5引入的concurrent包>中,曾经介绍过CountDownLatch.CyclicBarrier两个类,还给出了CountDownLatch的演示案例.这里再系统总结 ...

随机推荐

  1. Hark的数据结构与算法练习之计数排序

    算法说明 计数排序属于线性排序,它的时间复杂度远远大于常用的比较排序.(计数是O(n),而比较排序不会超过O(nlog2nJ)). 其实计数排序大部分很好理解的,唯一理解起来很蛋疼的是为了保证算法稳定 ...

  2. js:语言精髓笔记7----原型继承

    面向对象有三个基本特性:封装,继承,多态:如果都满足的话称为面向对象语言:而部分满足则称为基于对象语言: 数据类型实现模型描述: JavaScript对象模型: 构造过程:函数->构造器 构造器 ...

  3. WireShark数据包分析数据封装

    WireShark数据包分析数据封装 数据封装(Data Encapsulation)是指将协议数据单元(PDU)封装在一组协议头和尾中的过程.在OSI七层参考模型中,每层主要负责与其它机器上的对等层 ...

  4. 2^x mod n = 1

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  5. Js和asp.net各自设置的cookie相互读取的方法

       在Web的开发过程中,避免不了要使用cookie,在这里,我们在前台设置cookie,也可以在后台设置cookie,关键是在前后台设置的cookie,怎么去相互读取,代码如下:  (1)  前台 ...

  6. [leetCode][001] Maximum Product Subarray

    题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...

  7. 【BZOJ】1901: Zju2112 Dynamic Rankings(区间第k小+树状数组套主席树)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1901 首先还是吐槽时间,我在zoj交无限tle啊!!!!!!!!我一直以为是程序错了啊啊啊啊啊啊. ...

  8. WordPress折腾日记

    安装环境: 我开了个虚拟机xp....用xampp的整合包..下载地址https://www.apachefriends.org/zh_cn/download.html 跟着安装就行了.最后打开xam ...

  9. LCA的五种解法

    标准求法 //O(nlogn)-O(logn) #include<cstdio> #include<algorithm> using namespace std; ; ],to ...

  10. c++ map 的使用

    1.map是一类关联式容器,它是模板类. 关联的本质在于元素的值与某个特定的键相关联,而并非通过元素在数组中的位置类获取.它的特点是增加和删除节点对迭代器的影响很小,除了操作节点,对其他的节点都没有什 ...