Threads and synchronized methods

Discussion in 'Java' started by Luca D., Apr 30, 2009.

  1. Luca D.

    Luca D. Guest

    Let's suppose to have two threads, A and B, which use an object C:

    public class C {
    public synchronized void f1() {
    while(true) {
    wait();
    }
    }

    public synchronized void f2() {
    notifyAll();
    }
    }

    If A and B have THE SAME reference to the object C, and A calls f1(),
    can B call the mothod f2 to wake up A, or the entire object C is
    blocked because of the waiting thread (A)?
    Would it make any difference if A and B had two different instances of
    C?
    Thanks.
     
    Luca D., Apr 30, 2009
    #1
    1. Advertising

  2. Luca D. wrote:
    > If A and B have THE SAME reference to the object C, and A calls f1(),
    > can B call the mothod f2 to wake up A, or the entire object C is
    > blocked because of the waiting thread (A)?


    Read the documentation for the wait and notify* methods:

    wait():
    Causes the current thread to wait until either another thread invokes
    the notify() method or the notifyAll() method for this object, or a
    specified amount of time has elapsed.

    The current thread must own this object's monitor.

    This method causes the current thread (call it T) to place itself in the
    wait set for this object and then to relinquish any and all
    synchronization claims on this object.

    [ Note: if you call x.wait(), you must be synchronized on x for it to
    succeed, otherwise you get an IllegalMonitorStateException ]

    > Would it make any difference if A and B had two different instances of
    > C?


    Um... yes. You would be using two different objects for synchronization...

    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
     
    Joshua Cranmer, Apr 30, 2009
    #2
    1. Advertising

  3. Luca D.

    coffeymex Guest

    > If A and B have THE SAME reference to the object C, and A calls f1(),
    > can B call the mothod f2 to wake up A, or the entire object C is
    > blocked because of the waiting thread (A)?


    A thread releases the lock to the object it is synchonising one while
    it is waiting (and when it is woken, it re-acquires the lock before
    being
    able to continue).

    In case it's useful, some other information/examples I've written
    on wait/notify:

    http://www.javamex.com/tutorials/synchronization_wait_notify.shtml.

    Note that in most cases, it's really worth considering using one of
    the Java 5 concurrency utilities instead of wait/notify.

    Neil
     
    coffeymex, Apr 30, 2009
    #3
  4. Luca D.

    Mark Space Guest

    Luca D. wrote:

    > If A and B have THE SAME reference to the object C, and A calls f1(),
    > can B call the mothod f2 to wake up A, or the entire object C is
    > blocked because of the waiting thread (A)?
    > Would it make any difference if A and B had two different instances of
    > C?



    Does this look like a homework problem to anyone else?
     
    Mark Space, Apr 30, 2009
    #4
  5. Luca D.

    Lew Guest

    Luca D. wrote:
    >> If A and B have THE SAME reference to the object C, and A calls f1(),
    >> can B call the mothod f2 to wake up A, or the entire object C is
    >> blocked because of the waiting thread (A)?
    >> Would it make any difference if A and B had two different instances of
    >> C?


    Mark Space wrote:
    > Does this look like a homework problem to anyone else?


    I sure hope not, because the way the question is phrased, that would
    mean someone is teaching Java who totally doesn't understand it.

    --
    Lew
     
    Lew, Apr 30, 2009
    #5
  6. Luca D.

    Mark Rafn Guest

    Luca D. <> wrote:
    >public class C {
    > public synchronized void f1() {
    > while(true) { wait(); }
    > }
    > public synchronized void f2() { notifyAll(); }
    >}


    >If A and B have THE SAME reference to the object C, and A calls f1(),
    >can B call the mothod f2 to wake up A, or the entire object C is
    >blocked because of the waiting thread (A)?


    Nope. wait() releases the lock before waiting. When notified, it continues
    to be blocked until it can re-acquire the lock before continuing.

    >Would it make any difference if A and B had two different instances of
    >C?


    Obviously, as this.wait() and this.notifyAll() on two different objects refer
    to different 'this'es, so won't interact with each other at all.
    --
    Mark Rafn <http://www.dagon.net/>
     
    Mark Rafn, Apr 30, 2009
    #6
    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. Jerry
    Replies:
    4
    Views:
    131,847
    tonni
    Aug 11, 2010
  2. Pep
    Replies:
    6
    Views:
    29,280
  3. dmcreyno
    Replies:
    9
    Views:
    9,568
    Mark Space
    Jun 27, 2006
  4. ankur
    Replies:
    4
    Views:
    1,445
    Eric Sosman
    Nov 28, 2008
  5. Marcin Rodzik
    Replies:
    6
    Views:
    357
    Marcin Rodzik
    Aug 25, 2011
Loading...

Share This Page