• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread wait() and notifyall()

 
chitturi vinay babu
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 103
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic