Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

purpose of join  RSS feed

 
sushant prabhu
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i reckon join is used to when one thread waits for another to finish and then the former thread continues.

say if i hav created 3 child threads out of my main thread . i give 3 join methods in my main so that it waits till all 3 child threads finish their task and then the main thread resumes.

what i want to ask is even if i dont give join on the 3 threads inside main and finish main even before the 3 threads finish i dont find the other 3 threads getting interrupted.
shouldnt the 3 child threads which were churned out of main get interupted when the main method ends.
i mean that is what join method is used for
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No! That's the whole point of threads, they are independant threads of control.
They have no relationship with the main thread, so they can continue to run on their own after the main thread dies.
Now, there is the concept of a daemon thread. You can set a thread to be a daemon thread before you start it. When a thread is a daemon thread, it will continue to run until there are no more user threads running. At that point, the JVM will quit. Another way to state it is that, the JVM continues to run only as long as there are user threads running.
In your example, you could test this out by making each of the 3 threads you spawn a daemon thread, and making them all run infinate loops. Then let the main thread quit. When it does, you will see the whole JVM quits, even though the other 3 threads were still active.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depends on wether those spawned threads are daemon threads or not.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!