我想模拟使用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