File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in thread 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 "Doubt in thread" Watch "Doubt in thread" New topic

Doubt in thread

Frederico Benevides

Joined: Jan 15, 2006
Posts: 25
I don't know why it works, because I thought in "laurel.wait(1000);" the wait has to be inside in a synchronized block or synchronized method, but I don't understand why in here is not in this and it works.

Thank you.

And here, the wait has the problem because is not inside in a synchronized block.

[ March 08, 2006: Message edited by: Frederico Benevides ]
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
In the first program, you are calling the wait method from within a try block which has a corresponding catch block that ignores all checked exceptions. If you add a print statement in the catch block to print out the name of the exception, this is one possible output.

In the second program you are not catching and ignoring any checked exception so you get the IllegalMonitorStateException.
[ March 08, 2006: Message edited by: Keith Lynn ]
Frederico Benevides

Joined: Jan 15, 2006
Posts: 25
Wow, is true, how I didn't see that

thank you!
Stephen O'Kane

Joined: Aug 17, 2005
Posts: 26
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
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Doubt in thread