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 their execution.
Example:
Thread A waits until Threads B and C finishes their execution. After that Thread A must be executed and other threads should wait. This is a cycle that repeats n times.
ThreadA{
public void run(){
while(true){
if(queue.peek() % 2 == 0){
phaser.arriveAndAwaitAdvance();
doStuff();
}
}
}
}
ThreadB{
public void run(){
while(true){
if(queue.peek() % 2 == 0){
doStuff();
phaser.arrive();
}
}
}
}
Thread C has the same pseudocode as Thread B.
I need synchronized something like this: Thread A must executed only after Thread B and Thread C perform their execution and Thread B and Thread C must wait Thread A finalize its execution.
I tryied using Phaser as mentioned in code above with Phaser but I din't get the result expected.
Could someone give an example?
I am using Java 8 and I looking for a resettable form of CountDownLatch, maybe using phaser.