• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

join() method usage

 
nikita mishra
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This code makes the current thread (main method) to execute after the thread2 finishes its job. How to make thread2 to execute after thread1?









 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you have to use thread2.join() inside thread1.
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:
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?

 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:
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.....

 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ankit Garg!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic