Search code examples
javamultithreadingconcurrencyjava.util.concurrent

Run Java Threads sequentially


How will you execute Three threads sequentially? For eg. Thread1, Thread2, Thread3. It is not possible to pass the reference of one Thread to the other and invoke from the run() method.

So code should be like this:

 Thread1.start();
 Thread2.start();
 Thread3.start();

and out put should be

 Printing Thread1
 Printing Thread2
 Printing Thread3

This can be possible by using ThreadPoolExecutor and using a blocking queue but even that is not an acceptable answer.


Solution

  • Use ExecutorService in java.util.concurrent package. More precisely use Executors.newSingleThreadExecutor();