• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads..join() method

 
swaraj gupta
Ranch Hand
Posts: 182
C++ Java Oracle
  • Mark post as helpful
  • send pies
  • 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..
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • 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: 182
C++ Java Oracle
  • Mark post as helpful
  • send pies
  • 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
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • 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.
 
Rahul Saple
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • 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: 182
C++ Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Rahul...
 
suraj august
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • 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: 182
C++ Java Oracle
  • Mark post as helpful
  • send pies
  • 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.
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The call to join will cause UseJoin thread to wait for itself to complete thus it will never complete...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic