Results 1 to 14 of 14
  1. #1
    denis is offline Member
    Join Date
    Sep 2008
    Posts
    5
    Rep Power
    0

    Default about wait() and notifyALL

    hi everyone i have a question about using wait and notifyALL()
    i am very confused about these two which i have never been able to use.

    as per the API
    wait() = Causes current thread to wait until either another thread invokes the notify() method or the notifyAll().
    does the above wait() means that the current thread will wait as the monitor has already been in use by another thread. and will continue until the thread using the monitor releases the lock through notifyALL().

    notifyall() = is notify all the threads waiting on the monitor.

    i would really appreciate if somebody could post a simple code on wait and notifyALL().

    thanks in advance.
    Denis
    Last edited by denis; 09-25-2008 at 10:07 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,609
    Rep Power
    23

    Default

    post a simple code on wait and notifyALL().
    I did that just recently. Do a search for wait or notify.

    It's in this thread:
    Once again: waiting in a thread loop.
    Last edited by Norm; 09-25-2008 at 11:54 PM.

  3. #3
    Niveditha's Avatar
    Niveditha is offline Senior Member
    Join Date
    May 2008
    Posts
    307
    Rep Power
    6

    Default

    :) lots of examples in the net available do a google search.
    To finish sooner, take your own time....
    Nivedithaaaa

  4. #4
    Mrs. Deswal's Avatar
    Mrs. Deswal is offline Member
    Join Date
    Apr 2009
    Location
    Gurgaon,India
    Posts
    24
    Rep Power
    0

    Default

    Hi,

    I have also problem using wait() and notifyAll(). My program gives error in simply using wait(). Please tell me.

  5. #5
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    A MonitorStateException? You have to be synchronized on the object you're waiting on.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  6. #6
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    First, don't worry -- practically the entire world doesn't know how to use wait/notify. One thing to consider is whether for what you're doing there's something else in the concurrency library that'll help you. For example, BlockingQueue is a better alternative to many typical uses (the "producer-consumer" pattern), and if you want to use it for a thread pool, consider using ThreadPoolExecutor.

    OK, that said, the essential way it works is that you have some object which will act as the "message signaller". One or more threads will wait() on that object. That means they sit "waiting for a messge" telling them to wake up. Then at some point, another thread will call notify() or notifyAll() on that same object, which is what makes the waiting thread(s) wake up.

    - notify() makes one random thread wake up; it's useful for implementing thread pools, where you have a stream of "jobs" coming in and you want "any available thread" to take the job
    - notifyAll() wakes up all threads that were waiting on that object; it's useful in cases where all threads waiting on that object potentially need to be signalled.

    Note that the notified thread(s) don't necessarily wake up immediately-- just when it's next "convenient" for the thread scheduler to let them run.

    Both the waiter and the notifier must synchronize on the object. But while waiting, the waiter temporarily releases the lock, then re-acquires it when it is woken up.

    You may be interested in various articles I've written on wait/notify on my web site. They're some of the most consulted articles on the site-- as I say, it seems the world in general is a bit mystified by wait/notify.

  7. #7
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    Sorry, I should have given a concrete example. A typical case is you have a logging thread that sits waiting for strings to log. And then other threads post strings to that thread. So the logging thread's run method looks a bit like this. For clarity, I'm missing out exception handling (you need to catch interruptions, generally around the whole method code):

    Java Code:
    private List<String> messageQueue;
    ...
    public void run() {
      while (!stopRequested) {
        String line;
        synchronized (messageQueue) {
          while (messageQueue.isEmpty()) {
            messageQueue.wait();
          }
          line = messageQueue.remove(0);
        }    
        log 'line'
      }
    }
    Notice we wait in a while loop: it's conceivable that wait() could "spuriously return" even though you haven't been notified. So we repeatedly check if there was actually a message posted to the queue.

    Then when another thread wants to log a line, it calls a method such as this:

    Java Code:
    public void logLine(String line) {
      synchronized (messageQueue) {
        messageQueue.add(line);
        messageQueue.notifyAll();
      }
    }
    Here, we may as well use notifyAll() -- in general, it's best to use notifyAll() unless you know that the behaviour you want is to just wake up one random thread waiting on that object.

    I should stress though, that wait/notify is largely obsolete for most purposes like this. As of Java 5, BlockingQueue is a generally much preferable alternative.
    Last edited by neilcoffey; 04-20-2009 at 03:07 PM. Reason: added note about BlockingQueue

  8. #8
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    It doesn't "spuriously return". If you've called notifyAll() then all waiting threads are going to wake up and only one is going to get the lock. You need the loop to put all the rest to sleep again.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  9. #9
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    In this case, that's not really what the loop's for-- though obviously, it does have that function when there are competing threads for the object being removed (e.g. a thread pool).

    Note that wait() really is defined as being allowed to spuriously wake up!

  10. #10
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Hmm, so it is. All the more reason to do concurrency carefully.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  11. #11
    Mrs. Deswal's Avatar
    Mrs. Deswal is offline Member
    Join Date
    Apr 2009
    Location
    Gurgaon,India
    Posts
    24
    Rep Power
    0

    Default

    Hi,

    I want to read some files by a thread & want to write them to another file by another thread. I want this in manner that one thread read a file in a stringbuffer and another thread write them in a file and then first thread read second file so on. I have file paths in a string array. Please provide me code if possible.

    Thanks in advance.
    Sharing knowledge means gaining more knowledge.:)

  12. #12
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Did you address my previous suggestion?
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  13. #13
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    To do the one-thread-read, one-thread-write problem, the code is practically the code that I posted above, just instead of logging, the thread writes the string to a file, and the actual string posted to the "logging" thread is a string from the file. As I say, BlockingQueue is often a preferable alternative. There's some example code at this link that again is based around the idea of a logging thread, but is easily adapted to write the strings to a file.

  14. #14
    Mrs. Deswal's Avatar
    Mrs. Deswal is offline Member
    Join Date
    Apr 2009
    Location
    Gurgaon,India
    Posts
    24
    Rep Power
    0

    Default

    yaah,

    I have addressed your previous suggession. But i am not so much exprienced developer. So i was unable to do that.

    Thanks.
    Sharing knowledge means gaining more knowledge.:)

Similar Threads

  1. [SOLVED] Application should wait until a Dialog closes
    By hannehomuth in forum Advanced Java
    Replies: 2
    Last Post: 07-24-2008, 02:51 PM
  2. What is the execution path of wait() and notify() ?
    By AegisCruiser in forum Threads and Synchronization
    Replies: 1
    Last Post: 04-23-2008, 06:16 PM
  3. How to use sleep() to wait for a while
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 06:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •