Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

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.

 
Marshal
Posts: 25594
69
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.
 
I was born with webbed fish toes. This tiny ad is my only friend:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic