Questions tagged [phaser]

Introduced in Java 7, Phaser is a reusable synchronization barrier, similar in functionality to CyclicBarrier and CountDownLatch but supporting more flexible usage.

Introduced in Java 7, Phaser is a reusable synchronization barrier, similar in functionality to CyclicBarrier and CountDownLatch but supporting more flexible usage.

Not to be confused with Phaser.io, which uses the tag .

29 questions
30
votes
6 answers

Flexible CountDownLatch?

I have encountered a problem twice now whereby a producer thread produces N work items, submits them to an ExecutorService and then needs to wait until all N items have been processed. Caveats N is not known in advance. If it were I would simply…
Adamski
  • 54,009
  • 15
  • 113
  • 152
17
votes
3 answers

Java: tutorials/explanations of jsr166y Phaser

This question was asked two years ago, but the resources it mentions are either not very helpful (IMHO) or links are no longer valid. There must be some good tutorials out there to understand Phaser. I've read the javadoc, but my eyes glaze over,…
Jason S
  • 184,598
  • 164
  • 608
  • 970
4
votes
2 answers

How do I find out when the last party fires Phaser.arrive()?

Given: Executor executor = ...; Phaser phaser = new Phaser(n); for (int i=0; i
Gili
  • 86,244
  • 97
  • 390
  • 689
3
votes
3 answers

Understanding phaser in java with an example

I am trying to understand Phaser in java. I wrote an example which is stuck at advance waiting for other parties to arrive. As far as I understand, phaser is used as a reusable thread synchronization (unlike CountdownLatch which is not reusable)…
would_like_to_be_anon
  • 1,639
  • 2
  • 29
  • 47
3
votes
1 answer

ForkJoinPool, Phaser and managed blocking: to what extent do they works against deadlocks?

This little code snippet never finishes on jdk8u45, and used to finish properly on jdk8u20: public class TestForkJoinPool { final static ExecutorService pool = Executors.newWorkStealingPool(8); private static volatile long consumedCPU =…
3
votes
1 answer

How to create chain/tier of Phasers

I'm writing multithread application which uses Phaser to know when to finish work. The problem is that in ExecutorCompletionService there can be even 100k of Threads in a queue, but maximum number of inarrived parties in Phaser is 65535. What can I…
swch
  • 1,432
  • 4
  • 21
  • 37
2
votes
1 answer

Why C++ has introduced separate std::latch and std::barrier?

There are some similar questions on SO. But they only ask the question one way. std::latch has an advantage over std::barrier that unlike latter, former can be decremented by a participating thread more than once. std::barrier has an advantage over…
Sourav Kannantha B
  • 2,860
  • 1
  • 11
  • 35
2
votes
2 answers

Phaser Synchronization Usage

General Question It is well known that Phaser can be used to synchronize the start time of all tasks as mentioned in the JavaDocs and this blog by Niklas Schlimm. Niklas has drawn a pretty understandable image of the synchronization: …
HKTonyLee
  • 3,111
  • 23
  • 34
2
votes
1 answer

How to use Phaser correctly?

I have implemented MyListener which is accessed from two different threads. class MyListener implements Listener { private final Phaser phaser = new Phaser(2); @Override public void changed () { phaser.arrive(); } …
Nikolay Kuznetsov
  • 9,467
  • 12
  • 55
  • 101
1
vote
1 answer

Phaser issues (using JSR166y with latest JDK of version 6)

So, I've got this Phaser that is really flexible but it seems I am missing something. I have successfully used CyclicBarrier but now I also want something more flexible as I said. So here is the code: Declarations: private static final…
Kounavi
  • 1,090
  • 1
  • 12
  • 24
1
vote
0 answers

Resetable CountDownLatch

I have a problem that I want to solve. I have one thread that must wait a number of a variable number of async threads to finish their execution finishes and after that, It must execute and again stay in the wait state until the other threads finish…
1
vote
0 answers

Replace Phaser with CompletableFuture

I have a bunch of methods using Phaser (with always 1 party) and I could replace each method by using CompletableFuture instead. The result would be the same. Are there any hidden benefits when using CompletableFuture? For example: version 1 with…
nimo23
  • 5,170
  • 10
  • 46
  • 75
1
vote
1 answer

Registering thread to Phaser

I am learning about Phaser. While doing so, I came across a problem. Below is the code that I have, public class RunnableTask implements Runnable { private Phaser phaser; public RunnableTask(Phaser phaser) { this.phaser = phaser; …
1
vote
2 answers

Phaser - how to use it as CountDownLatch(1)?

I understand I could simply use CountDownLatch directly, however, as an excercise and to understand Phaser better, I would like to use it instead of COuntDownLatch. Thus, I would create N number of awaiters, and one thread that needs to flip the…
Bober02
  • 15,034
  • 31
  • 92
  • 178
1
vote
1 answer

Using and reusing Phaser instead of join()

I have to wait in main thread for termination of working threads, then (on some event) I start those threads and wait for termination again, etc. Calling join() works well, but I want to do it faster. Phaser looks like what I'm searching for, but…
jakson
  • 325
  • 2
  • 4
  • 12
1
2