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?
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.
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.