Harpreet Singh janda wrote:you have to use thread2.join() inside thread1.
Not exactly, if you want to start thread2 after thread1 completes, then you'll have to change the lines 34 and 35 i.e.
to
If you want thread2 to wait for thread1 to finish but main thread to complete without interruptions, then thread2 needs a reference to thread1 and call the join method on that reference...
Harpreet Singh janda wrote:you have to use thread2.join() inside thread1.
Not exactly, if you want to start thread2 after thread1 completes, then you'll have to change the lines 34 and 35 i.e.
to
If you want thread2 to wait for thread1 to finish but main thread to complete without interruptions, then thread2 needs a reference to thread1 and call the join method on that reference...
See the coding :
I make it to Thread1 - first, Thread2 - second, Main - 3. But in some cases, I didn't get the whole Output of Thread1?
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
But in some cases, I didn't get the whole Output of Thread1?
I don't know how you created and compiled the program, but for me thread1's output always completes before thread2 starts...
That's a mistake of mine.... Sorry....
Ankit wrote :
If you want thread2 to wait for thread1 to finish but main thread to complete without interruptions, then thread2 needs a reference to thread1 and call the join method on that reference...
Hi, You mean this.....
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Abimaran, I didn't exactly mean the same thing but its almost the same as your code. Your code is also going to stop the main method till thread1 completes, because you have a join call in the main method. Even if you remove that join call, the main method will wait for thread1 to complete because you called join in the constructor of Thread2 class. I was talking about this
Now the main method will complete without any waiting and thread2 will only start when thread1 is complete...