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.
You could use Executors.newSingleThreadExecutor(), but strictly speaking this launches only one Thread
, so may not be expected solution.
The simpliest solution using just Thread
class:
Thread1.start();
Thread1.join();
Thread2.start();
Thread2.join();
Thread3.start();
Thread3.join();
(I omitted exception handling for clarity, Thread.join()
can throw InterruptedException
)