Reputation: 7197
Let us assume we have a method:
public class DoStuff implements Runnable {
public void run()
{
A();
B();
C();
}
}
Is there a simple way to create n
threads that will run all A(), then all B(), then all C()?
I know that I can do something like:
ExecutorService app = Executors.newFixedThreadPool(threadCount);
//create A class and run A();
app.shutdown();
app.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
For A() then B() then C(), but I want something like the following:
public class DoStuff implements Runnable {
public void run()
{
A();
//wait all A to finish;
B();
//wait all B to finish;
C();
}
}
Using the same threads all the time.
Upvotes: 2
Views: 86
Reputation: 26313
You are looking for CyclicBarrier
https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CyclicBarrier.html
Upvotes: 4
Reputation: 1376
you need to make A, B and C as threads and you need to join them. Join A with B and B with C.
In this way you are making B thread to wait until A's job is done and C thread to wait until B's job is done.
Upvotes: -1