1 2 Previous Next 17 Replies Latest reply: Dec 28, 2009 6:16 AM by JosAH RSS

    What happens if  notify() is actived before  wait() method??

    843789
      That!!

      "What happens if notify() is actived before wait() method??"

      I've read that method acts like a counter, or rather if there are several notify() before waiting threads, they are marks by all those notify(), then the next threads that call a wait() method from the object won't block themselves.

      Is true that sentence? I haven't found anything about it in docs or other pages.
        • 1. Re: What happens if  notify() is actived before  wait() method??
          794069
          You should take Josuha Bloch's advice when it comes to notify() and wait()

          [Effective Java|http://books.google.com/books?id=ka2VUBqHiWkC&pg=PT295&lpg=PT295&dq=Joshua+bloch+notify&source=bl&ots=yXIgRen2V0&sig=nJcDgEpAVht2TrlU_pTaIxhqHD8&hl=en&ei=MxM3S8LTJ922jAetgbGmDg&sa=X&oi=book_result&ct=result&resnum=1&ved=0CAgQ6AEwAA#v=onepage&q=&f=false]
          • 2. Re: What happens if  notify() is actived before  wait() method??
            JosAH
            molavec wrote:
            "What happens if notify() is actived before wait() method??"
            It'll be a cry in the dark because nothing is ready to be notified; it doesn't harm either.

            kind regards,

            Jos
            • 3. Re: What happens if  notify() is actived before  wait() method??
              843789
              JosAH wrote:
              molavec wrote:
              "What happens if notify() is actived before wait() method??"
              It'll be a cry in the dark because nothing is ready to be notified; it doesn't harm either.

              kind regards,

              Jos
              Even in the dark, your cry would have woke up some implementation's book mark staff.

              Quoted in JLS
              17.8.1 Wait

              1. Thread t is added to the wait set of object m, and performs n unlock actions on
              m.
              2. Thread t does not execute any further instructions until it has been removed
              from m's wait set. The thread may be removed from the wait set due to any
              one of the following actions, and will resume sometime afterward.
              ...
              ...
              An internal action by the implementation. Implementations are permitted,
              although not encouraged, to perform ``spurious wake-ups'' -- to remove
              threads from wait sets and thus enable resumption without explicit instructions
              to do so. Notice that this provision necessitates the Java coding practice
              of using wait only within loops that terminate only when some logical
              condition that the thread is waiting for holds.
              • 4. Re: What happens if  notify() is actived before  wait() method??
                JosAH
                w.rodent wrote:
                JosAH wrote:
                molavec wrote:
                "What happens if notify() is actived before wait() method??"
                It'll be a cry in the dark because nothing is ready to be notified; it doesn't harm either.
                Even in the dark, your cry would have woke up some implementation's book mark staff.

                Quoted in JLS
                17.8.1 Wait

                1. Thread t is added to the wait set of object m, and performs n unlock actions on
                m.
                2. Thread t does not execute any further instructions until it has been removed
                from m's wait set. The thread may be removed from the wait set due to any
                one of the following actions, and will resume sometime afterward.
                ...
                ...
                An internal action by the implementation. Implementations are permitted,
                although not encouraged, to perform ``spurious wake-ups'' -- to remove
                threads from wait sets and thus enable resumption without explicit instructions
                to do so. Notice that this provision necessitates the Java coding practice
                of using wait only within loops that terminate only when some logical
                condition that the thread is waiting for holds.
                That section only applies to the discipline of doing this:
                while (!<condition>)
                   wait();
                It doesn't say anything about just doing this in another thread:
                notifty(); // or notifyAll();
                When no other threads are waiting the above will just be a cry in the dark (i.e. nothing happens);

                kind regards,

                Jos
                • 5. Re: What happens if  notify() is actived before  wait() method??
                  843789
                  while (!<condition>)
                     wait();
                  Then why this kind of code need to "spurious wake-ups'' , or could you explain under what situation the thread calling wait would be resumed without any notify()? Show me some demo.

                  I think the scenario for "the Java coding practice
                  of using wait only within loops that terminate only when some logical
                  condition that the thread is waiting for holds" is like the following.


                  Thread1 check the condition
                  Thread2 set the condition
                  Thread2 Notify
                  Thread1 Wait

                  If there is no book-keeping of the prior Notify, the Thread1 would be waiting for a long non-sense time.If the waiting thread could be resumed in this case, it could loop back to the start to recheck the condition.
                  • 6. Re: What happens if  notify() is actived before  wait() method??
                    843789
                    It is just that there are chances that notify() called prior to wait() would have some side effect, which is up to specific implementations.

                    Hope I am some right.
                    • 7. Re: What happens if  notify() is actived before  wait() method??
                      JosAH
                      w.rodent wrote:
                      while (!<condition>)
                      wait();
                      Then why this kind of code need to "spurious wake-ups'' , or could you explain under what situation the thread calling wait would be resumed without any notify()? Show me some demo.
                      If another thread does a notifyAll() a thread may wake up (while in a wait() state) that shouldn't wake up, hence the loop.
                      I think the scenario for "the Java coding practice
                      of using wait only within loops that terminate only when some logical
                      condition that the thread is waiting for holds" is like the following.

                      Thread1 check the condition
                      Thread2 set the condition
                      Thread2 Notify
                      Thread1 Wait
                      Thread 1 would not be woken up because it wasn't waiting yet at the time of the notify(). Notify() can't look into the future and waits() don't wake up because of a notify() that happened before the wait() happened.

                      kind regards,

                      Jos
                      • 8. Re: What happens if  notify() is actived before  wait() method??
                        843789
                        JosAH wrote:
                        If another thread does a notifyAll() a thread may wake up (while in a wait() state) that shouldn't wake up, hence the loop.
                        You are not making any sense here. It is legitimate for a thread to wake up when notifyAll() invoked on the wait object. Have you ever read through the bold statement I quoted from JLS?

                        JosAH wrote:
                        Thread 1 would not be woken up because it wasn't waiting yet at the time of the notify(). Notify() can't look into the future and waits() don't wake up because of a notify() that happened before the wait() happened.

                        kind regards,

                        Jos
                        Exactly, that is what's called "spurious wake-ups'', I suppose.
                        • 9. Re: What happens if  notify() is actived before  wait() method??
                          EJP
                          This is all irrelevant. If there are no threads waiting on an object, calling notify() or notifyAll() on it does nothing.
                          • 10. Re: What happens if  notify() is actived before  wait() method??
                            JosAH
                            w.rodent wrote:
                            JosAH wrote:
                            If another thread does a notifyAll() a thread may wake up (while in a wait() state) that shouldn't wake up, hence the loop.
                            You are not making any sense here. It is legitimate for a thread to wake up when notifyAll() invoked on the wait object. Have you ever read through the bold statement I quoted from JLS?
                            Yes I have; multiple times and years ago already.

                            >
                            JosAH wrote:
                            Thread 1 would not be woken up because it wasn't waiting yet at the time of the notify(). Notify() can't look into the future and waits() don't wake up because of a notify() that happened before the wait() happened.

                            kind regards,

                            Jos
                            Exactly, that is what's called "spurious wake-ups'', I suppose.
                            No, a notify() before any wait() doesn't do anything; it doesn't hang around just in case in the future another thread might perform a wait(). All the documentation says is that an implementation can do a notify() without the user code starting the notify(). The implementation might want to rearrange its queues or whatever. Therefore a woken up thread should always check whether or not its condition was met.

                            kind regards,

                            Jos
                            • 11. Re: What happens if  notify() is actived before  wait() method??
                              3004
                              w.rodent wrote:
                              It is just that there are chances that notify() called prior to wait() would have some side effect, which is up to specific implementations.
                              Nope.
                              • 12. Re: What happens if  notify() is actived before  wait() method??
                                843789
                                Thanks for your answers guys and sorry for reply you so late. Anyway, your answers confirm and example that I made for test that situation.

                                The idea was made a signals system that notify one thread (threadShowSocket) when other thread (ThreadRecieveConnection) receive a connection wake up the the first one.
                                public class LogSystem {
                                     
                                     Object NewConnectionAlert, NewTransferConnectionAlert;
                                     
                                         public LogSystem()
                                     {
                                          *socketNewConnection = null; *
                                          socketNewTransferConnection = null;
                                          
                                          *NewConnectionAlert = new Object();*
                                          NewTransferConnectionAlert = new Object();
                                     }
                                
                                        public void notifyNewConnection(Socket s) {
                                          socketNewConnection = s;
                                          synchronized(NewConnectionAlert){
                                               NewConnectionAlert.notify();
                                          }
                                     }
                                
                                        public Socket waitNewConnection() {
                                          synchronized(NewConnectionAlert){
                                                try {
                                                    NewConnectionAlert.wait();
                                               } catch (InterruptedException e) {
                                                    e.printStackTrace();
                                               }
                                          }
                                          return socketNewConnection;
                                     }
                                // I repeated last two methods for other kind of connection
                                
                                }
                                Finally, I tested this:
                                threadShowSocket call -> LogSystem.waitNewConnection();

                                Other program generated a lot of clients that generates a lot of notify() from ThreadRecieveConnection.

                                Results:

                                *>wait()*
                                *>notify()*
                                *>notify()*
                                *>notify()*
                                *>notify()*
                                *>Show one socket*
                                *>wait();*

                                The program didn't escape from wait() in spite of there was a lot notify() before the last wait();

                                The guy that make a tutorial that I have read, committed an error!!
                                • 13. Re: What happens if  notify() is actived before  wait() method??
                                  843789
                                  Sorry about the divert, it turns out that the "Spurious wakeup" is not that relative to notify() as I fabricated.
                                  • 14. wait/notify and spurious wakeups
                                    782681
                                    w.rodent wrote:
                                    Sorry about the divert, it turns out that the "Spurious wakeup" is not that relative to notify() as I fabricated.
                                    Relation of spurious wakeups to notify() is explained in [bug 4308396|http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4308396|Sun bugs database] Java wait/notify should require condition variable, allow spurious wakeups
                                    The current Java Language Specification #) definition of wait, notify,
                                       and notifyAll does not require that they be done within a "condition
                                       variable" paradigm.  The requested change would be to require that
                                       wait be called from within a loop that tests for a logical condition
                                       variable, and that notify and notifyAll change the value of that
                                       condition variable, and that when wait returns the value of the condition
                                       variable must be checked to see that it has changed, otherwise the
                                       loop continues and the wait is reentered.  A corollary of the requested
                                       change is that waits may return due to spurious wakeups, meaning operating
                                       system events unconnected to any action within the Java program.
                                    #) current Java Language Specification referred above is actually previous edition; this old bug is fixed in current JLS

                                    Edited by: gnat on Dec 28, 2009 10:28 AM
                                    1 2 Previous Next