8 Replies Latest reply: Jan 6, 2005 3:03 PM by 796440 RSS

    Threads

    807596
      Hi,

      If Object.wait( ) method is called from a synchronized method,then the current thread will move to the waiting state until some other thread invokes the notify ( ) or notifyAll ( ) method on this object.

      Can any body tell me that when the thread moves to the waiting state,will it release the lock ?

      Thanks !
        • 1. Re: Threads
          796447
          Can any body tell me that when the thread moves to
          to the waiting state,will it release the lock ?
          It will if it calls wait() on the same object that it is synchronized() on. But I believe it will not otherwise.
          • 2. Re: Threads
            807596
            If it will not release the lock,then how will another thread call notify ( ) method without acquiring the lock ?
            • 3. Re: Threads
              796447
              If it will not release the lock,then how will another
              thread call notify ( ) method without acquiring the
              lock ?
              The other thread does acquire the lock (via synchronized(o)) on the same object that another thread released it on (via o.wait()). What does this have to do with my answer? I guess either I wasn't very clear, or you otherwise misunderstood. I thought you asked about basically this:
              synchronized(x) {
                y.wait(); <-- does this release x's lock? Answer: No
              }
              synchronized(x) {
                x.wait(); <-- does this release x's lock? Answer: Yes
              }
              • 4. Re: Threads
                796447
                does this release x's lock? Answer: No
                Clarification: No, unless y == x
                • 5. Re: Threads
                  796440
                  Can any body tell me that when the thread moves to
                  to the waiting state,will it release the lock ?
                  It will if it calls wait() on the same object that it
                  is synchronized() on. But I believe it will not
                  otherwise.
                  If you call wait on an object you're not synced on, you'll get IllegalMonitorStateException.
                  • 6. Re: Threads
                    796440
                    synchronized(x) {
                    y.wait(); <-- does this release x's lock? Answer:
                    r: No
                    }
                    Throws IllegalMonitorStateException unless y==x or this code is inside a block that's synced on y.
                    • 7. Re: Threads
                      796447
                      Throws IllegalMonitorStateException unless y==x or
                      this code is inside a block that's synced on y.
                      Of course. But that wasn't what I was talking about. It is assumed the monitor is already held by perhaps an outer sync block. This was all about addressing what happens to other monitors when one calls wait() on another.
                      • 8. Re: Threads
                        796440
                        Throws IllegalMonitorStateException unless y==x or
                        this code is inside a block that's synced on y.
                        Of course. But that wasn't what I was talking about.
                        It is assumed the monitor is already held by perhaps
                        an outer sync block.
                        I didn't have that assumption, and I didn't get that the OP had it either.
                        This was all about addressing
                        what happens to other monitors when one calls wait()
                        on another.
                        Yeah, but given the initial question in this thread, I felt the OP might be confused if it wasn't explicitly spelled out.