This is the part of my code,I am facing problems in using wait()
class Leader extends Thread {
private Table table;
synchronized public void run()
{
while(true)
{
try
{
while(table.getResources()!=0)
wait();
table.putResources(5); // here I am updating the value(I have a table class)
} catch(InterruptedException e) { }
}
}
}
class Soldier extends Thread {
public void run()
{
while(true)
{
try
{
synchronized(this)
{
if(table.getResources()==this.need_Value) //checking value of resource
{
sleep(1000);
table.clearResources(); //this will put the value of resource to 0
notifyAll();
}
}
} catch(InterruptedException e) { }
}
}
}
So basically I have a thread of leader class and a thread of Leader class. Initially resources is 0 so in the Soldier class resources is updated to 5. Suppose now for Soldier class need_Value was 5,so it will update value of resources to 0. So now Leader class should again run as resources is 0,but actually it is still waiting. So is there anything wrong with my wait()?
PS- Assume I have Table class and all the constructors and variables used. I have omitted them because the code was too long