1
我正在實現一個帶有三個類的虛擬程序,以更直觀地參考Future
的工作方式。我的問題是,有時程序將被鎖定在同步方法,它不會繼續。我找不到原因。有人能夠發現爲什麼沒有一個戰士會打印「我贏了」的路線嗎?當塊同步方法塊執行
我的電流輸出:
Fighter1
Fighter1已進入同步方法
Fighter2
Fighter2已進入同步方法
的代碼如下。
主要類:
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class TmpTest {
private static final ExecutorService executorService =
Executors.newFixedThreadPool(2);
public static void main(final String... args) {
final Fighter fighter1 = new Fighter("Fighter1");
final Fighter fighter2 = new Fighter("Fighter2");
final Future<String> submitFighter1 = executorService.submit(fighter1);
final Future<String> submitFighter2 = executorService.submit(fighter2);
while (!submitFighter1.isDone() || !submitFighter2.isDone()) {
if (submitFighter1.isDone()) {
System.out.println("Fighter 1 wins!");
submitFighter2.cancel(true);
executorService.shutdown();
} else if (submitFighter2.isDone()) {
submitFighter1.cancel(true);
System.out.println("Fighter 2 wins!");
executorService.shutdown();
}
}
}
}
戰鬥機類:
class Fighter implements Callable<String> {
private final String fighterName;
private final ClassWithSyncMethod classWithSyncMethod;
public Fighter(final String fighterName) {
this.fighterName = fighterName;
classWithSyncMethod = new ClassWithSyncMethod(fighterName);
}
@Override
public String call() throws Exception {
return classWithSyncMethod.syncMethod();
}
}
虛擬類synchronized方法:
class ClassWithSyncMethod {
private final String fighterName;
public ClassWithSyncMethod(final String fighterName) {
this.fighterName = fighterName;
}
public synchronized String syncMethod() {
System.out.println(fighterName + " has entered the sync method");
try {
Thread.sleep(1000);
} catch (final InterruptedException e) {
System.out.println("Exception trying to sleep the fighter " + fighterName + ";" + e);
}
return fighterName + " shouts: I won!";
}
}
你是絕對正確的。我是個白癡。謝謝! – Tavo
右鍵點'每個戰士在不同的物體上同步',爲你+1 –