我想模擬使用CyclicBarrier
的triatlon競爭,但它沒有按預期工作,我不知道爲什麼。CyclicBarrier不按預期工作
比賽的每一部分都要等到所有的跑步者都完成了前一個跑步者,但看起來好像是在等待着。
這是第一階段的一段代碼:
class Runner implements Runnable
{
private CyclicBarrier bar = null;
private static int runners;
private static double[] time;
private int number;
public static String name;
public Runner(int runners, String name)
{
time = new double[runners];
for (int i=0; i<runners; i++)
time[i] = 0;
this.name= name;
}
public Runner(CyclicBarrier bar, int number)
{
this.bar = bar;
this.number = number;
}
public void run()
{
try { int i = bar.await(); }
catch(InterruptedException e) {}
catch (BrokenBarrierException e) {}
double tIni = System.nanoTime();
try { Thread.sleep((int)(100*Math.random()); } catch(InterruptedException e) {}
double t = System.nanoTime() - tIni;
time[number] += t;
}
}
public class Triatlon
{
public static void main(String[] args)
{
int runners = 100;
CyclicBarrier Finish_Line_1 = new CyclicBarrier (runners);
Runner c = new Runner(runners, "Triatlon");
ExecutorService e = Executors.newFixedThreadPool(runners);
for (int i=0; i<runners; i++)
e.submit(new Runner(Finish_Line_1, i));
System.out.println(Finish_Line_1.getNumberWaiting()); // this always shows 99
try { int i = Finish_Line_1.await(); }
catch(InterruptedException e01) {}
catch (BrokenBarrierException e02) {}
System.out.println("Swimming phase completed");
// here the rest of the competition, which works the same way
}
}
你明白了。我想補充一點,我認爲它應該是'Thread.sleep(...)'然後'barrier.await()'在每個運行方法中。事實上,跑步者跑步,然後等待其他人加入 – Grooveek
@Grooveek這會更有意義,是的 - 並且會掩蓋OP的錯誤,因爲然後池中的一個線程會卡住。根據代碼的其餘部分,錯誤可能會在下一個階段中出現,並且起源更加混亂。 –
謝謝!這給我的問題提供了一個解決方案。雖然我看到一個異常。印刷完「游泳階段完成」後,程序沒有完成,就好像它正在等待其他事情發生一樣。 – dabadaba