• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Thread class join() method

 
Ranch Hand
Posts: 59
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Please explain the working of the below code. I can't understand how the join() method works here. This code I tried to understand the working of join() method in the Thread class.

 
Sheriff
Posts: 24635
56
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you tell us where you got that code from?
 
nitin sethi
Ranch Hand
Posts: 59
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul, I downloaded an ms doc of questions from a site a month ago. I don't remember what site that was.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i added logs to your code so it's easier to see whats happening.



The output I get is:
x now is 5
Calling join on Thread-0 from main
Starting thread: Thread-0
Thread-0 is going to sleep for 10 seconds.
Thread-0 has finished sleeping for 10 seconds.
main can continue working.
9

So you can see that:
You have 2 threads working: "Thread-0" created by your call to "new MyThread()" and "main" created by the JVM.
The call to setAndPrintX() is running on "main" as you can see by calling Thread.currentThread().getName() inside the method.
Calling join() inside setAndPrintX() is the same as calling this.join() so join() is called on the anonymous instance of MyThread created in "main" by "new MyThread()".
So in effect the call to join() can be read as:
"main" will wait for "Thread-0" to finish, and then it will continue.

Rodrigo

 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Join method is little tricky , how I understand is that " a thread which calls join method on another thread object wait for that to complete and then joins him i.e. start running)



here main thread waits until Thread a finished execution , by join you achieve sequential processing.
 
Live ordinary life in an extraordinary way. Details embedded in this tiny ad:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!