• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread class join() method

 
Ranch Hand
Posts: 59
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic