0
private void stopThread() {
thread1.canRun(false);
}
private void createThread() {
thread1.stopThread();
thread1.canRun(true);
thread1 = new Thread(thread1);
t.start();
}
進出口創造了戰艦的比賽中,線程啓動,當用戶用他的船射擊,他有n秒拍攝,如果他們用完了,他失去了遊戲.. 當他投籃最後一個線程停止並開始一個新的線程。這就是爲什麼我在創建線程中調用停止線程。2個線程運行一個比另一個
public void canRun(boolean canrun) {
this.canrun= canrun;
}
public thread(player player, gamej) throws RemoteException {
this.player= player;
this.j = j;
tiempoTimeOut = j.getTimeout();
}
@Override
public void run() {
try {
for (int i = 0; i <= TimeOut && canRun; i++) {
// System.out.println(i);
Thread.sleep(1000);
if (i ==tiempoTimeOut) {
j.notifiy();//the player lost code irrelevant
}
}
}
的問題是,當我stopthread的爲不應該繼續運行和線程完成,但我認爲當我開始新的線程canRun(真)遙遙領先「爲」結構。 所以它改變了可變方式的第一個線程完成 因此它會繼續運行,因爲canRun是回真正的
我認爲解決的辦法是利用地方syncrhonized前 但林不知道.. 幫助謝謝
你有你的聲明變量canrun揮發性? –
爲什麼'createThread'正在執行'thread1.canRun(true)'?你創建一個新的線程下面,可能可以運行 – zapl
它不能運行,因爲遍歷「for」一個條件是canRun = true – AlanRubinoff