Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why no compilation failure

 
O. Ziggy
Ranch Hand
Posts: 430
Android Debian VI Editor
  • Mark post as helpful
  • send pies
  • 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
 
fred rosenberger
lowercase baba
Bartender
Posts: 12203
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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 Debian VI Editor
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 12203
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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 Debian VI Editor
  • Mark post as helpful
  • send pies
  • 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?
 
naveen yadav
Ranch Hand
Posts: 384
Java MyEclipse IDE Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Marshal
Pie
Posts: 21514
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
 
Andreas Svenkson
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic