Re: How to use and notify
How are you using the wait and notify methods?
Re: How to use and notify
the method that writes into list, the list is called OutChannel
Code :
synchronized(OutChannel)
{
DataItem.DataField =Data;
DataItem.Valid=true;
OutChannel.Push(DataItem);
OutChannel.notify();
}
and the method that reads from list, the list called InChannel
Code :
synchronized(InChannel)
{
while(InChannel.Size()==0)
try
{
InChannel.wait();
}catch (InterruptedException e){}
Poped=InChannel.Pop();
System.out.println("Poped: " + Poped.DataField + " " + Poped.Valid);
if (!Poped.Valid)
{
return null;
}
else
return (Poped.DataField);
the names are different in threads C1 and C2, but both pointers points to one list, and these pointer are set in C4. in the above code if C2 executes InChannel.wait(), all my program waits. but I want just C2 waits and C4 runs the C3 after that C1 and again C2 and C3 and so on
Re: How to use and notify
Do the variables: InChannel and OutChannel refer to the same object? wait and notify won't work if they are different objects.
The use of wait and notify requires that they be used with the same object. Read the Object class doc for those two methods.
Re: How to use and notify