次のように、プログラムに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が設定されます。true1 番目のスレッドで使用しない場合、2 番目のThread.sleep(1000)スレッドで afterCONNECTION_ESTABLISHEDが設定されている場合、1番目のスレッドでは関連するステートメントをtrue入力しません。if
これを回避する別の方法はありますか?私の最初のスレッドは、多くの場合、2 番目のスレッドの変数の変更に依存するためです。