This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
1) when the yield, sleep or join() called on a thread, shd they be necessarily within the try catch construct? any compile time error?
2) can a thread unyield itself?
3) can a thread unjoin itself?
1) join and sleep yes. Because they throw InterruptedException which is a checked exception. All checked exceptions must be caught or a compile time error is produced. Join() doesnt throw any such exception so therefore you dont need to do anything.
2) 3) no although an other thread can call the interrupt() method. I dont see how a thread can do anything while it isnt doing anything.
(1)Since the two methods namely sleep() and join()throws InterruptedException which is a checked exception, it either needs to be caught and handled or should be again thrown that is the only alternative.
(2)About unyielding, once a thread decides to be yielded by invoking the static method Thread.yield() then it is under the mercy of the thread scheduler. i.e. the CPU time would be alloted to that thread when the scheduler decides and in no way the thread unyield itself.
(3)Unjoining, here when the thread imvokes the join() then it is pushed to a "non runnable state". So there is no question of the thread to perform any operation including unjoining!! (a) if the joined thread runs to completion (b) if the specified time expires(see the argument of join(long ms)) (c) if interrupted by any other thread by invoking interrupt() on that thread.
Remember I) By Thread.yield(), the thread moves to "ready to run (Runnable state)" and not "nonrunnable state".
(II) And join() will push the thread to "nonrunnablestate".