• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

K&B: Question on Threads-Selftest.

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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. }
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for the input.

Regards.
 
Stop it! You're embarassing me! And you are embarrassing this tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic