• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Threads..join() method

 
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have few doubts in my mind regarding the join() method,

1> join() causes the currently executing thread to move into which state:
a> runnable(ready)state
b> Not-runnable state

2> If "t.join()" causes the currently executing thread to stop until the thread referenced by t finish its execution, then what does only join() on line#8 mean in the following example,which I have tried recently:
Does it mean "join the thread executing the run() method to itself i.e. the thread will wait for itself to finish its own execution.(will it be deadlock then..?)". Am I right here?
Please help..
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For your Doubt-1 - It goes to waiting State.

For the Second Doubt: What did you get when you executed that program?
 
swaraj gupta
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mohamed sanaullah wrote:For your Doubt-1 - It goes to waiting State.

You mean non-runnable state wait.?

For the Second Doubt: What did you get when you executed that program?

The second println() statement of run() method is not executing. (The JVM is not moving ahead to execute it)
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

swaraj gupta wrote: You mean non-runnable state wait.?


Is there a Non-Runnable state? Waiting state- the Thread is not in a Runnable state.

swaraj gupta wrote:
The second println() statement of run() method is not executing. (The JVM is not moving ahead to execute it)



JVM is confused what to do I dont know the exact reason for this behavior.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

join() causes the currently executing thread to move into which state:


It causes the thread to move into a non-runnable state(Waiting for join completion State) which is represented by the constant Thread.WAITING(Enum in the Thread Class)

what does only join() on line#8 mean in the following example


You are right on this one, it waits for itself to finish and waits forever since the join method doesnt take an arguement.
Replacing the join with a one which takes an arguement makes the program deadlock-free.

The thread would now move into a Thread.TIMED_WAITING state for whatever duration it is waiting.
Hope that helped.
 
swaraj gupta
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Rahul...
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here the waiting thread is the main thread .The main thread will wait for the useJoin thread .as when you do t.join the current thread stops and waits for t to stop.
 
swaraj gupta
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

suraj aryan wrote:Here the waiting thread is the main thread .The main thread will wait for the useJoin thread .as when you do t.join the current thread stops and waits for t to stop.


No Suraj, you are wrong. The join() method is a static method of Thread class and is always be executed for the thread executing it, that's why useJoin thread will be the waiting thread here, not the main thread.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

swaraj gupta wrote:The join() method is a static method of Thread class.


No its not. Check the docs of Thread class...
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The call to join will cause UseJoin thread to wait for itself to complete thus it will never complete...
 
Live a little! The night is young! And we have umbrellas in our drinks! This umbrella has a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic