• 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

using wait() method in Threads

 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ranchers,

I have a doubt regarding the self test question 16 in K&B 5.0
Please find the code below:

1. public class Test {
2. public static void main (String [] args) {
3. final Foo f = new Foo();
4. Thread t = new Thread(new Runnable() {
5. public void run() {
6. f.doStuff();
7. }
8. });
9. Thread g = new Thread() {
10. public void run() {
11. f.doStuff();
12. }
13. };
14. t.start();
15. g.start();
16. }
17. }
1. class Foo {
2. int x = 5;
3. public void doStuff() {
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. }
15. }
16. }

Answer given is An Exception occurs at runtime.

my doubt is regarding the call to wait() method.
wait() method can be called only in synchronized context right.
But here in this example, there is a call to wait() method in a non synchronized method/block. But when i tried to compile the code in my IDE, it compiles properly.

Can any one please explain me why the code given above compiles?

Thanks,Srilatha M
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you call wait on an object whose lock you don't hold then you get an exception at runtime and not error on compilation...
 
M Srilatha
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ankit,

THe following stmts are from K&B 5.0

"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."

My Doubt is that why the code compiles when the method wait() has been called in a non-synchronized context.

Thanks,Srilatha M
 
author
Posts: 23951
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
Where in the K&B statement that states that this is a compile time check? In fact, the part of the statement that says "unless it owns that object's lock" implies that the application is running -- hence, a runtime check.

Henry
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the check for wait is on run time and not compile time

suppose if the check was on compile time, then this would be flagged as an error

void func()
{
this.wait();
}

the compiler must flag this as an error,which it doesn't, as the method may have been called from a synchronized context

synchronized void func2()
{
func();
}
OR
void func2()
{
synchronized(this)
{
func();
}
}

So the compiler is not sure at compile time that the method func() can call wait() method. Because of this it allows the method func() to call method wait(and notify,notifyAll) anywhere and if at the time of call to the method i.e. wait if the thread doesn't hold lock on the object on which the wait method is called, then an Exception is thrown...

I hope you must have got it now....
 
M Srilatha
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Now my doubt is cleared.

@Henry:
In K&B, its not mentioned as compiler error. You are right. I was just confused about the call to wait() method.

Thanks,Srilatha M
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic