Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

Subtle difference in wait / notify

Thread view: 
stixwix - 30 Nov 2005 14:50 GMT
Hi,
Can anyone explain this behaviour? It is basically the minimum amount
of code to set up a thread that waits until it is notified by the main
thread.
In this code the notify does not work because of the way it uses an
instance of the Outer class to specify the Runnable passed to the
Thread constructor.
However, if the first two lines of the test method are replaced by:
Thread t = new Thread(new Worker());
it works as expected.

public class Outer {
   private Object lock = new Object();
   public class Worker implements Runnable {
       public void run(){
           synchronized(lock){
           try {
               lock.wait();
           }
           catch (InterruptedException e) {}
            }
        }
   }
   public void test(){
       Outer out = new Outer();
    Thread t = new Thread(out.new Worker());
    t.start();
    // allow enough time for the thread to enter wait
    Thread.sleep(100);
    synchronized(lock) {
       lock.notifyAll();
    }
   }
}
Thomas Hawtin - 30 Nov 2005 15:06 GMT
> Can anyone explain this behaviour? It is basically the minimum amount
> of code to set up a thread that waits until it is notified by the main
> thread.

Where did you get that code from?

> In this code the notify does not work because of the way it uses an
> instance of the Outer class to specify the Runnable passed to the
> Thread constructor.
> However, if the first two lines of the test method are replaced by:
> Thread t = new Thread(new Worker());
> it works as expected.

With the out.new Worker() version you are using a different Outer and
hence lock object for the wait than for the notify. The new Worker()
version, with implicit this., uses the same object for both wait and notify.

> public class Outer {
>     private Object lock = new Object();
[quoted text clipped - 19 lines]
>     }
> }

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Thomas Fritsch - 30 Nov 2005 15:21 GMT
> Hi,
> Can anyone explain this behaviour? It is basically the minimum amount
[quoted text clipped - 28 lines]
>        lock.notifyAll();
>     }
You synchronize and notify on the wrong lock object (not the same as
used in your wait). When you change the above 3 line to
        synchronized(out.lock) {
            out.lock.notifyAll();
        }
it will work again.
>     }
> }

Signature

"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.