私はいくつかのコードについてアドバイスを得ることができるかどうか疑問に思っていました
OKなので、スレッド化されたクラス(非常に基本的な)が基本的にこのオブジェクトを呼び出し、そのコードが今表示されます...このコードは私に無限のwait()を与えており、理由がわかりません。
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;
}
コードをできる限り単純化しましたが、非常に単純なプログラムですが、1週間前にマルチスレッドを始めたばかりで、多くの概念がまだ混乱しています。このプログラムが行うことは、基本的に、すべてのスレッドが実行する前に一定量のボールが必要であり、必要なボールがない場合は、利用可能になるまでキューに待機することです。