//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. Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) B. Bear and Three Musketeers 枚举

                                          B. Bear and Three Musketeers                                   ...

  2. 【bzoj1051】 [HAOI2006]受欢迎的牛 tarjan缩点判出度算点数

    [bzoj1051] [HAOI2006]受欢迎的牛 2014年1月8日7450 Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B ...

  3. 【codevs2216】行星序列 线段树 区间两异同修改+区间求和*****

    [codevs2216]行星序列 2014年2月22日3501 题目描述 Description “神州“载人飞船的发射成功让小可可非常激动,他立志长大后要成为一名宇航员假期一始,他就报名参加了“小小 ...

  4. http://blog.sina.com.cn/s/blog_5f103c9c0101atny.html

    http://blog.sina.com.cn/s/blog_5f103c9c0101atny.html http://www.oschina.net/question/117304_51525

  5. POJ3250 Bad Hair Day(单调栈)

    题目大概就是给一个序列,问每个数右边有几个连续且小于该数的数. 用单调递减栈搞搞就是了. #include<cstdio> #include<cstring> using na ...

  6. Rain on your Parade

    Rain on your Parade Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 655350/165535 K (Java/Ot ...

  7. java.lang.RuntimeException: Invalid action class configuration that references an unknown class named [xxxAction]。

    java.lang.RuntimeException: Invalid action class configuration that references an unknown class name ...

  8. sprintf() in c

    Declaration Following is the declaration for sprintf() function. int sprintf(char *str, const char * ...

  9. WP7.1 应用程序发布到Marketplace

    从8月22起Windows Phone marketplace可以提交7.1 sdk开发的应用了,尽管提交页面和方式与7.0是同一个,但是还是会出现一些问题.并且在提交之前也注意一些问题. 7.0 应 ...

  10. stack, deque 和 queue的对比

    stack, deque 和 queue这三个c++的STL的数据结构很类似但又各有不同. stack是堆栈,没有迭代器,特点是后进先出.用push()将元素压入栈中,top()返回栈顶元素,pop( ...