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.
I have a question about this code example (TwoThreads class). It comes from K&B's book, page 756.
Given the above code which letters will eventually appear somewhere in the output? (choose all that apply.)
A. A B. B C. C D. D E. E F. F G. The answer cannot be reliably determined H. The code does not compile
the answer is:
A, C, D, E, F.
I don't understand why B is not a correct answer, as is it not possible that a InterruptedException (or any other random exception) occurs, therefore B might be printed? Or are these sort of things ignored in the exam. I thought the answer would be G, as it cannot be relibly determined if B will be output or not.
Plus, the book says that on the exam, the number of options for an answer will be given. In this case, does this mean that in the exam they will tell us 5 answers are correct? That would make this question infinitely more obvious.
Thanks for your time Sok
[ July 30, 2006: Message edited by: Stephen O'Kane ]
[ July 30, 2006: Message edited by: Stephen O'Kane ] [ July 30, 2006: Message edited by: Stephen O'Kane ]
when its hardy time to run and it calls wait method on the terminated thread laurel, it throws exception and prints B.
No B is never ever outputted like the book says youre not calling sleep on a the other thread, its a static method therefore whatever thread its called in will sleep. now AFAIK, the sleep() does throw an interruptedexception but for that to happen, some other thread must call the interrupt(). here even if you change the catch above S.O.P(B) to catch an InterruptedException and try to display B it wont because the laurel thread is not being interrupted. Again more input from ranchers would be appreciated.
One thing I didn't understand is that if the wait() method is not called from a synchronized context, the compiler should give error, right? So it should not even compile.
Joined: Apr 25, 2005
wait() method throws IllegalMonitorStateException if the current thread is not the owner of the object's monitor i.e., if wait() method is not called within a synchronized method or in a synchronized block.
Ok, my brain understands it now, thanks Amitabha. After having a look at the API for InterruptedException;
Thrown when a thread is waiting, sleeping, or otherwise paused for a long time and another thread interrupts it using the interrupt method in class Thread.
It seems that the only way for the piece of code where sleep() is called to throw an exception is if interrupt() is called on the thread that was executing when sleep() was called. Since no interrupt is called in the code, then we can assume no exception is thrown (I hope).
Thanks everyone Sok
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com