Results 1 to 7 of 7
- 07-19-2012, 09:08 PM #1
Member
- Join Date
- Jul 2012
- Posts
- 3
- Rep Power
- 0
Threads wait() ¬ify() 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(); }
- 07-19-2012, 11:02 PM #2
Re: Threads wait() ¬ify() 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!
- 07-19-2012, 11:34 PM #3
Member
- Join Date
- Jul 2012
- Posts
- 3
- Rep Power
- 0
Re: Threads wait() ¬ify() 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-19-2012 at 11:51 PM.
- 07-20-2012, 04:21 AM #4
Re: Threads wait() ¬ify() 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!
- 07-20-2012, 09:18 AM #5
Member
- Join Date
- Jul 2012
- Posts
- 3
- Rep Power
- 0
Re: Threads wait() ¬ify() 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 09:42 AM.
- 07-20-2012, 06:21 PM #6
Re: Threads wait() ¬ify() 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!
- 07-20-2012, 09:37 PM #7
Similar Threads
-
Threads synchronization with wait() & notify() problem
By Cropcircles in forum Threads and SynchronizationReplies: 4Last Post: 03-25-2012, 10:44 PM -
Wait() and Notify()
By SiX in forum New To JavaReplies: 15Last Post: 07-28-2011, 04:29 PM -
wait() and notify()
By jomypgeorge in forum New To JavaReplies: 4Last Post: 02-15-2011, 08:58 AM -
Need help with wait and notify
By mityay in forum Threads and SynchronizationReplies: 3Last Post: 01-06-2011, 04:24 PM -
Need help with wait() and notify()
By Mkaveli in forum Threads and SynchronizationReplies: 2Last Post: 03-30-2010, 11:58 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks