Dan's exam question

Question 5 Which of the following will force a thread to move into the Not-Runnable state? a. Thread.yield method. b. Thread.sleep method. c. Thread.join method. d. Object.wait method. e. By blocking on IO. f. Unsuccessfully attempting to acquire a lock on an object. g. None of the above. I selected d,e,f but the answer is b c d e f. Well what I think is that if Thread.sleep() is called with an argument of 0 it will not cause the thread to get into the Not-Runnable state. If Thread.join is used on a thread and if that thread has already completed then also I guess that the thread will not get into the Not-Runnable state. Though I also selected the Object.wait() method how would it behave when called with an argument of 0.

Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1090

posted May 16, 2003 15:15:00

0

I have the same doubts for question no. 15. I am now giving the Dan's exam so if i find some doubts I will post it. [ May 16, 2003: Message edited by: Anupam Sinha ]

my answer to this question was sleep, wait, block on IO, unsuccessfull attempt to acquire lock.... but I fail to understand why join()? definitely not yield(), coz I realised that yield() may cause the thread to yield to another or may be bought back to running state by the scheduler... join() is where the main thread waits for the currently referenced thread to die... the only reason , why join may cause the thread to be non -runnable is becoz it may be waiting or blocked on IO or one of the above reasons... Dan can you explain this please

Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1090

posted May 18, 2003 14:07:00

0

join() is where the main thread waits for the currently referenced thread to die... the only reason , why join may cause the thread to be non -runnable is becoz it may be waiting or blocked on IO or one of the above reasons... Dan can you explain this please

Yes join may cause a thread to wait if the thread is in the above states but it may also wait if the thread is not in the above states. Simply if the the thread on which join is invoked is doing some work but is not in the above mentioned states. [ May 18, 2003: Message edited by: Anupam Sinha ]

The instance join method is used to make one Thread (let's call it A) wait for another (let's call it B). So, when A invokes B.join(), A becomes Not-Runnable. Note that join does not cause A to relinquish the locks it holds on objects.

SCJP 1.4, SCWCD 1.3, SCBCD 1.3

Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1090

posted May 18, 2003 15:22:00

0

Hi Roger What you said is true. But what if thread A's A.join() is invoked after the thread A has completed its run method. That is from thread B invokes A.join() but before this statement is executed A has already completed its work. Secondly what if you invoke A.join(0) with an argument of 0. Hope that helps.

Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683

posted May 18, 2003 15:42:00

0

But what if thread A's A.join() is invoked after the thread A has completed its run method. That is from thread B invokes A.join() but before this statement is executed A has already completed its work.

If A's run method has finished, then A is dead and cannot be resurrected. You can still invoke A's methods, but they will run in another thread.

Secondly what if you invoke A.join(0) with an argument of 0.

join(0) is the same as join(), ie causes the thread to wait forever. If B completes, then the thread scheduler can put A into the running state again.