aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads..join() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads..join() method" Watch "Threads..join() method" New topic
Author

Threads..join() method

swaraj gupta
Ranch Hand

Joined: Oct 22, 2010
Posts: 181

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

Joined: Sep 08, 2007
Posts: 3071
    
  33

For your Doubt-1 - It goes to waiting State.

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


Mohamed Sanaulla | My Blog
swaraj gupta
Ranch Hand

Joined: Oct 22, 2010
Posts: 181

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

Joined: Sep 08, 2007
Posts: 3071
    
  33

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

Joined: Aug 02, 2006
Posts: 46
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

Joined: Oct 22, 2010
Posts: 181

thanks Rahul...
suraj aryan
Greenhorn

Joined: Nov 19, 2010
Posts: 17
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

Joined: Oct 22, 2010
Posts: 181

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

Joined: Aug 03, 2008
Posts: 9305
    
  17

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

No its not. Check the docs of Thread class...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

The call to join will cause UseJoin thread to wait for itself to complete thus it will never complete...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads..join() method