次のように、プログラムに2つのスレッドがあります。
class SendThread implements Runnable {
public void run(){
Thread.sleep(1000);
while (true){
if (CONNECTION_ESTABLISHED){
// code here
}
}
}
}
class ReceiveThread implements Runnable {
public void run(){
while (true){
// code here
CONNECTION_ESTABLISHED = true;
}
}
}
として定義CONNECTION_ESTABLISHED
しましたstatic Boolean
。
2 番目のスレッドでは、特定の時点でBoolean
CONNECTION_ESTABLISHED
が設定されます。true
1 番目のスレッドで使用しない場合、2 番目のThread.sleep(1000)
スレッドで afterCONNECTION_ESTABLISHED
が設定されている場合、1番目のスレッドでは関連するステートメントをtrue
入力しません。if
これを回避する別の方法はありますか?私の最初のスレッドは、多くの場合、2 番目のスレッドの変数の変更に依存するためです。