• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K&B: Question on Threads-Selftest.

 
Tommaso Nuccio
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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. }
 
megha joshi
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21437
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tommaso Nuccio
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for the input.

Regards.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic