This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why no compilation failure Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why no compilation failure " Watch "Why no compilation failure " New topic
Author

Why no compilation failure

O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430



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

Joined: Oct 02, 2003
Posts: 11152
    
  16

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


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

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

Joined: Oct 02, 2003
Posts: 11152
    
  16

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

Joined: Oct 02, 2005
Posts: 430

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

Joined: Oct 23, 2011
Posts: 384

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
Sheriff

Joined: Sep 28, 2004
Posts: 18504
    
  40

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


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

Joined: Jan 17, 2011
Posts: 179
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Why no compilation failure
 
Similar Threads
Regular expression - Greedy and Reluctant quantifiers
Accessing instance members before the super constructor completes
Local variable scope
Calling a generic method generically still produces warnings.
Generics - Super type parameters