Subtle difference in wait / notify

Discussion in 'Java' started by stixwix, Nov 30, 2005.

  1. stixwix

    stixwix Guest

    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();
    }
    }
    }
     
    stixwix, Nov 30, 2005
    #1
    1. Advertising

  2. stixwix wrote:
    > 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();
    > 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();
    > }
    > }
    > }


    Tom Hawtin
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Nov 30, 2005
    #2
    1. Advertising

  3. stixwix wrote:
    > 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();
    > }

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


    --
    "Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')
     
    Thomas Fritsch, Nov 30, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stee1HeD

    j2me thread wait/notify question

    Stee1HeD, Aug 9, 2003, in forum: Java
    Replies:
    3
    Views:
    11,388
    Stee1HeD
    Aug 12, 2003
  2. Abraham Khalil

    Theads problem - wait and notify

    Abraham Khalil, Sep 3, 2003, in forum: Java
    Replies:
    1
    Views:
    540
    Roedy Green
    Sep 3, 2003
  3. Renato Battaglia
    Replies:
    3
    Views:
    2,841
    Jon Skeet
    Oct 24, 2003
  4. eeyimaya
    Replies:
    3
    Views:
    594
    eeyimaya
    Feb 18, 2004
  5. Jeff

    wait/notify cpu cost?

    Jeff, Apr 21, 2004, in forum: Java
    Replies:
    5
    Views:
    2,389
    Chris Uppal
    Apr 23, 2004
Loading...

Share This Page