wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B: Question on Threads-Selftest. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B: Question on Threads-Selftest." Watch "K&B: Question on Threads-Selftest." New topic
Author

K&B: Question on Threads-Selftest.

Tommaso Nuccio
Ranch Hand

Joined: Dec 11, 2006
Posts: 66
Hi there,

in the selftest of chapter9(SCJP 5), Question 16:
(I cutted some code)


one answer here is:
B. the code will not compile because of an error in Line 7 of class Foo.


but the correct answer is
E. An exception occurs at runtime.


Now I am confused, because on page 719 of the same book it says:
wait() [...] must be called from within a synchronized content!


My prolem is that I remember having answered mock exams, whith exactly the same problem, and the correct answer was a compile time error.
Of course I also checked the above answer by writing an own test and it doesn't give a compile time error.

But when in the exam, then in this case, I'd go for the runtime exception.
Would be great to hear some comments, especially from people that already made the exam.

Many thanks in advance.


Ciao,<br /> Tommaso<br /> <br />~*~*~*~<br />There are 10 types of people, those who understand binary and those who don't.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Yes, it's true that you need to call wait() from within a synchronized context. However, that it is not checked at compile-time.

Since the Object class has a wait() method, all class definitions inherit the wait() method, so it's legitimate to call the wait() method at compile-time.
Sam Sunamin
Ranch Hand

Joined: Mar 16, 2007
Posts: 113
wait();

The answer is E since wait() method has to been called in an synchronized block. Otherwise, runtime exception IllegalMonitorStateException



Before you call wait(), in this example, you have to own the lock of the object Foo's this.

using like:
synchronized(this){
4. if (x < 10) {
5. // nothing to do
6. try {
7. wait();
8. } catch(InterruptedException ex) { }
9. } else {
10. System.out.println("x is " + x++);
11. if (x >= 10) {
12. notify() ;
13. }
14. }


Yours Sam<br />SCJP5.0 97%<br />SCBCD5.0 72%
megha joshi
Ranch Hand

Joined: Feb 20, 2007
Posts: 206
Thanks for the post, I too was under the impression that lack of synchronization gives compile time error in case of wait.

But now,

I think there is a compile time error when wait()(also sleep(),join()) are called without the try/catch or Exception declaration.

Theres a Exception when wait()(also notify(),notifyAll()) are called without the synchronized block.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18538
    
  40

Thanks for the post, I too was under the impression that lack of synchronization gives compile time error in case of wait.


Unfortunately, it is actually not possible to check for synchronization at compile time, especially for public methods. Since it is possible for code to synchronize an object externally, then call a method of that object, it is possible for the wait() method to work in that method, even though it doesn't synchronize internally.

I think there is a compile time error when wait()(also sleep(),join()) are called without the try/catch or Exception declaration.


For checked exceptions, it is possible to check at compile time. If the checked exception is to be caught by the caller then the method must be declared as throwing that exception.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Tommaso Nuccio
Ranch Hand

Joined: Dec 11, 2006
Posts: 66
Thank you all for the input.

Regards.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: K&B: Question on Threads-Selftest.
 
Similar Threads
book K&B self test q16........problem
Selftest question
K&B Study Guide for Java 5 p498 Selftest problem 1
KB book selftest (question on Generics and Collections )
Runnable object!?