i was just wondering if i could get some advice on some code
OK so i have a threaded class (very basic) basically calls this object whose code i will show just now... this code is giving me a infinite wait() and i don't know why.
public void play()
{
if(!queue.isEmpty()){
synchronized(this)
{
if(queue.peek().ballCount <=AvailableGolfBalls)
{
//check if there all people in queue, if yes, give them preferance
queue.poll().notify();
}
}
}
hasBalls=false;
try
{
while (!hasBalls)
{
if(AvailableGolfBalls >= count)
{
AvailableGolfBalls -=count;
synchronized(this){
//the main code for thread
}
hasBalls=true;
}
else
{
//there isnt enough balls,wait
queue.add(this);
Thread.sleep(500);
System.out.println(ThreadID.get() +" -no balls availiable ");
synchronized(this)
{
this.wait();
}
}
}
}
catch (InterruptedException exception)
{
}
AvailableGolfBalls +=count;
}
i simplified my code as much as i could, its a ridiculously simple program though, but i just started with multi threading a week ago and a lot of the concepts still confuse me. what this program does is essentially is every thread needs a certain amount of balls before it can run, if it doesn't have the required balls, stand in queue until its available.
if(!hasBalls)akaif(woman)if(hasBalls) canMultiThread(); else cantMultiThread();