• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why no compilation failure

 
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Should this not have failed because wait() can only be called from a synchronised context?
What exactly will happen in the above case?

Thanks
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean "What will happen"? Did you TRY it? Why don't you tell us what happened?
 
O. Ziggy
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:What do you mean "What will happen"? Did you TRY it? Why don't you tell us what happened?



As i mentioned in my original post, i was expecting it to fail because i thought wait must be called from a synchronised context.
There was no compilation failure when i run it hence why i asked what exactly was happening.
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where does it say that wait can only be called from a synchronized method? I don't see it in the API for the object.wait() method, but I could be missing it.

So my guess is that "what will happen" is that it will compile and not produce errors. And in fact, that is exactly what happened when I compiled it.\

And we may be having a slight language issues...to me, "run it" means doing this:

java ZiggyTest2



The phrase 'compile it' means this

javac ZiggyTest2.java

 
O. Ziggy
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:

And we may be having a slight language issues...to me, "run it" means doing this:

java ZiggyTest2



The phrase 'compile it' means this

javac ZiggyTest2.java



Apologies for the language confusion. English is not my first language so i do apologies if my choice of words confused you. What i meant to say was that i compiled and run the program and there was no compilation failure or any runtime exception.

Here is a quote i read from the K&B book. Page 747, chapter 9 Threads (See under the "Thread interaction" heading).


One key point to remember (and keep in mind for the exam) about wait/notify is this:

wait(), notify() and notifyAll() must be called from within a synchronized context! A thread can't invoke a wait or notify method on an object unless it owns that object's lock.



Based on above i decided to write that small program to try and see what happens if wait() was called from a non-synchronised context.

- I was expecting it to either fail or to generate a runtime exception but none of the two happened.

To make things even more confusing: Have a look at this example



Running the above example generates an IllegalMonitorStateException. Why is the above code generating an IllegalMonitorStateException but the previous code does not?
Also if i modify the above code such that the wait() method is not enclosed inside a try/catch block i get a compiler error saying that InterruptedException must be declared or thrown. Again why did i not get this compilation failure on the original code example?
 
Ranch Hand
Posts: 384
MyEclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

O. Ziggy wrote:
One key point to remember (and keep in mind for the exam) about wait/notify is this:

wait(), notify() and notifyAll() must be called from within a synchronized context! A thread can't invoke a wait or notify method on an object unless it owns that object's lock.



wait() is a method in Object class ,so any object can invoke that.

As far as error or Runtime-Exception is concerned , please read above code carefully. "A thread can't invoke a wait or notify method on an object unless it owns that object's lock."
you haven't invoked wait() on thread.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

O. Ziggy wrote:
Running the above example generates an IllegalMonitorStateException. Why is the above code generating an IllegalMonitorStateException but the previous code does not?



How do you know that your previous code does not? It catches all exceptions and does nothing with it. If it throws the illegal monitor state exceptions, how will it show up?

Henry
 
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You probably know this by now, but as Henry elequently pointed out, your first piece of code catches all exceptions. As for the book explanation, yes I can see why you might expect a compiler error and not an exception, although all you really need to know is written in the API for Object's wait() method:

IllegalMonitorStateException - if the current thread is not the owner of this object's monitor.



// Andreas
 
reply
    Bookmark Topic Watch Topic
  • New Topic