0

Sorry if this is a stupid question. I found a solved problem in a book, but the solution doesn’t contain a source code. I proposed a code, but I’m not sure if the code fits the solution specifications. Please correct the code or propose a better code if possible (explanations are welcome). Thanks for the help.


Problem :

There are three threads T1, T2 and T3. How do you ensure sequence T1, T2, T3 in Java?


Solution :

Sequencing in multi-threading can be achieved by different means but you can simply use join() method of thread class to start a thread when another one is finished its execution. To ensure three threads execute you need to start the last one first e.g. T3 and then call join methods in reverse order e.g. T3 calls T2.join, and T2 calls T1.join, this ways T1 will finish first and T3 will finish last.


Proposed code :

final Thread t3 = new Thread(new T3()); // assume T3 is a Runnable
final Thread t2 = new Thread(new T2());
final Thread t1 = new Thread(new T1());

t3.start();
t2.start();
t1.start();

t2.join();
t1.join();
user654
  • 1
  • 3
  • 6
    The problem is really not well-stated. As stated, the best answer is to rearchitect to use only a single thread and just do the three things in the order they need to be done. – David Schwartz Jul 02 '15 at 17:51
  • Are you trying to ensure that they start in a particular order, that the complete in a particular order, or that they start and complete in order? – Erick G. Hagstrom Jul 02 '15 at 17:53
  • Create thread T2 at the end of the code for thread 1. Create thread T3 at the end of the code for thread 2. That, or just don't bother with more than one thread as suggested by others. I'm pretty sure that this silly requirement is is multi-duped anyway. – Martin James Jul 02 '15 at 22:31
  • It is: http://stackoverflow.com/questions/11544843/interview-how-to-ensure-that-a-thread-runs-after-another – Martin James Jul 02 '15 at 22:38

2 Answers2

4

Your proposed code won't work. t3 could finish before t2 even starts. To guarantee they run in sequence, you need to to this:

final Thread t3 = new Thread(new T3()); // assume T3 is a Runnable
final Thread t2 = new Thread(new T2());
final Thread t1 = new Thread(new T1());

t1.start();
t1.join();
t2.start();
t2.join();
t3.start();

But then using multiple threads is pointless and wasteful. You would achieve the same effect with a single thread:

new T1().run();
new T2().run();
new T3().run();
erickson
  • 265,237
  • 58
  • 395
  • 493
  • Or, t1 could finish before t3 starts. (It probably won't happen that way, but the JLS _permits_ it to happen that way.) The name `start()` is a little bit misleading: `t.start()` creates the new thread, and makes it runnable, but it does not actually wait for the new thread to start running. – Solomon Slow Jul 03 '15 at 12:07
1

In your example main thread will wait until execution of t2 and t1 is completed. Sequence of execution of these 3 threads could be different. I beleive that you want to start every thread after execution of the previous one is completed. In this case, I don't see the point of using parallelism. If you want to wait until execution of multiple threads is completed I could suggest you to read about barrier synchronization and especially this class

https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CyclicBarrier.html