Results 1 to 7 of 7
  1. #1
    mariki is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Default Threads wait() &notify() deadlock

    I have a question:
    I have "main-thread" that has a while loop where it check for condition (if more "sub-threads" are needed) then it spawns new "sub-tread" and goes to wait().

    When a "sub-thread" finishes it work it calls notify().

    The problem is that sub thread finishes and sends notify right after the "main-thread" evaluates condition and before calling wait(), this causes a dead lock.

    Portion of the code:

    Java Code:
    while(IsNewThreadNeed()){
       createAndStartThread();
       goWait();
    
    }
    
    
    
     public synchronized void goWait() {
        try {    		    		
        	   wait();
         } catch (InterruptedException e) {
        		
         }
    }
        
        
    public synchronized void goNotify() {
        	notify();
    }

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Threads wait() &notify() deadlock

    When you call wait() you release all locks held by the current thread. If you start the other thread and then call wait() within the same synchronized block, the other thread cannot obtain the lock to call notify() until the current thread has called wait().
    Get in the habit of using standard Java naming conventions!

  3. #3
    mariki is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Default Re: Threads wait() &notify() deadlock

    there are no locks for main-thread and sub threads.
    Main thread spawns sub-threads and goes to wait ,actually I think that I can remove the "synchronized" from goWait() method because only main thread can go to sleep, and only sub-threads can call notify()


    public synchronized void goNotify() {
    notify();
    }
    this method should synchronized I think because many sub-threads can call it.
    Last edited by mariki; 07-20-2012 at 12:51 AM.

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Threads wait() &notify() deadlock

    Yes, all your thread are using locks. When a thread enters a synchronized method, it obtains the lock (a.k.a. monitor) on the object the method belongs to. And a thread must hold the lock on an object to call wait() or notify() on it, or else you'll get an IllegalMonitorStateException.
    Get in the habit of using standard Java naming conventions!

  5. #5
    mariki is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Default Re: Threads wait() &notify() deadlock

    I don't understand, sub_threads are able to call notify even if the main thread is not in wait().
    if it's in wait() and sub-thread calls notify it wakes up, if it is not in wait state() and sub_thread call notify() nothing happens

    Scenario 1:
    Main thread is in while loop (no lock there), condition is true so it enters the loop spawns and starts new thread.
    Before it calls wait() the new thread can finish and call notify(), and then call wait().
    Because there are no more sub_threads we get a deadlock.

    Scenario 2:
    Main thread already spawned 2 sub treads and currently in wait().
    One of the threads finished it's work and calls notify().
    Main thread wakes up, evaluates while's condition , the condition is true. Main thread doesn't start a new thread (there is another check that i didn't mention in post) , but before it enters wait(), second sub thread calls notify() (that does not do anything), and now main thread calls wait() => dead lock


    Note: The while loop is in one object.
    The goWait() and goNotify() are in second object.(this object is shared)
    Last edited by mariki; 07-20-2012 at 10:42 AM.

  6. #6
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Threads wait() &notify() deadlock

    Scenario 1 would be fixed by the suggestion in my first comment. Scenario 2 is a bit more complicated. I'd need to see more of your code.
    Get in the habit of using standard Java naming conventions!

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Threads wait() &notify() deadlock

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Threads synchronization with wait() & notify() problem
    By Cropcircles in forum Threads and Synchronization
    Replies: 4
    Last Post: 03-25-2012, 11:44 PM
  2. Wait() and Notify()
    By SiX in forum New To Java
    Replies: 15
    Last Post: 07-28-2011, 05:29 PM
  3. wait() and notify()
    By jomypgeorge in forum New To Java
    Replies: 4
    Last Post: 02-15-2011, 09:58 AM
  4. Need help with wait and notify
    By mityay in forum Threads and Synchronization
    Replies: 3
    Last Post: 01-06-2011, 05:24 PM
  5. Need help with wait() and notify()
    By Mkaveli in forum Threads and Synchronization
    Replies: 2
    Last Post: 03-30-2010, 12:58 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
  •