Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes TwoThreads problem with exception 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 "TwoThreads problem with exception" Watch "TwoThreads problem with exception" New topic
Author

TwoThreads problem with exception

Stephen O'Kane
Greenhorn

Joined: Aug 17, 2005
Posts: 26
Hi Everyone,

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 ]
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
B is not correct as thread will return from sleep method after one second whereas, wait() method throws exception as current thread is not the owner of the lock.

Naseem


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Stephen O'Kane
Greenhorn

Joined: Aug 17, 2005
Posts: 26
Thanks Naseem,

I know that it will return from the sleep state after one second, but is it not possible that an exception could be thrown during that one second, causing 'B' to be printed?

Sok
Rishi Kanth
Ranch Hand

Joined: May 23, 2006
Posts: 43
when its hardy time to run and it calls wait method on the terminated thread laurel, it throws exception and prints B.

Rishi
Amit Batra
Ranch Hand

Joined: Mar 04, 2006
Posts: 361
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.
Surendra Kumar
Ranch Hand

Joined: Jul 04, 2006
Posts: 235
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.
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
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.

check wait() method in Object class

Naseem
Stephen O'Kane
Greenhorn

Joined: Aug 17, 2005
Posts: 26
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
 
Don't get me started about those stupid light bulbs.
 
subject: TwoThreads problem with exception