aspose file tools*
The moose likes Threads and Synchronization and the fly likes Thread wait() and notifyall() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread wait() and notifyall()" Watch "Thread wait() and notifyall()" New topic
Author

Thread wait() and notifyall()

chitturi vinay babu
Greenhorn

Joined: Oct 26, 2003
Posts: 1
In a multithreaeded programme i have a problum of notifying all the threads on a Object.


The code fragment that i fallowed for waiting is

test obj = new test(){
public void waitProcess()
{
synchronized(this)
{
try
{
wait();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
public void notifyAllProcess()
{
synchronized(this)
{
try
{
notifyAll();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
};

this single instance of object is "obj" is a global variable which is available for all the threads all the threads will first go to wait state and will be activated only after calling notifyby thread x.

my problum is :
as the block is synchronized block only one thread say "A" can call wait() and all other thread who want wait on that object need to wait until "A" comes outof the synchronized block.

if i need to notifyAll() the threads on "obj" object weather waiting thread "A" only notified or All the threads will be notified.
if all the threds are notified how can it is possible.

Plz find me the solution.
Daniel Mayer
Ranch Hand

Joined: Sep 09, 2004
Posts: 103
Originally posted by chitturi vinay babu:
as the block is synchronized block only one thread say "A" can call wait() and all other thread who want wait on that object need to wait until "A" comes outof the synchronized block.


No, calling wait releases the lock on the monitor, so you won't face this problem.
sanjeevmehra mehra
Ranch Hand

Joined: Aug 21, 2004
Posts: 75
notifyAll would rlease the lock on monitor & race condition b/w all the thread would lead to control over monitor, and only one thread would get control over the monitor and rest of the thread will wait for the control over the monitor.

thanks & regards,
Sanjeev.
[ September 16, 2004: Message edited by: sanjeevmehra mehra ]

thanks & regards,<br />Sanjeev.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
when a thread gets a lock on some object using synchronized block. it means no other thread, ofcourse using the same instance, can enter that block until it comes out or call wait().
[ September 14, 2004: Message edited by: adeel ansari ]
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Originally posted by sanjeevmehra mehra:
notifyAll would rlease the lock on monitor & race condition b/w all the thread would lead to control over monitor, and only one thread would get control over the monitor and rest of the thread will wait for the control over the monitor.



notifyAll does not release locks, only wakes up threads.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread wait() and notifyall()