gpt4 book ai didi

java - 线程对于某些值提前停止

转载 作者:太空宇宙 更新时间:2023-11-04 14:06:28 25 4
gpt4 key购买 nike

背景

因此,我正在编写一个应用程序,旨在执行蒙特卡罗模拟,以研究可以通过莫兰过程(进化图论)进化的图。对于无向图,这非常有效,但对于有向图,应用程序一直表现出奇怪的行为,我一生都无法弄清楚为什么。似乎发生的情况是,当这个 boolean 变量 isDirected 设置为 true 时,线程在满足循环条件之前退出它们运行的​​ for 循环,尽管当 isDirected 为 false 时工作正常。

这些图由邻接矩阵表示,因此当图有向时,代码中的唯一区别是邻接矩阵是非对称的,但我看不出任何会产生影响的原因。

代码

主要相关代码是 Controller 中的这一部分:

 //Initialise a threadPool and an array of investigators to provide each thread with an Investigator runnable
long startTime = System.nanoTime();
int numThreads = 4;
Investigator[] invArray = new Investigator[numThreads];
ExecutorService threadPool = Executors.newFixedThreadPool(numThreads);

//Assign the tasks to the threads
for(int i=0;i<numThreads;i++){
invArray[i] = new Investigator(vertLimit,iterations,graphNumber/numThreads,isDirected,mutantFitness,vertFloor);
threadPool.submit(invArray[i]);
}
threadPool.shutdown();

//Wait till all the threads are finished, note this could cause the application to hang for the user if the threads deadlock
try{
threadPool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
}catch(InterruptedException except){
System.out.println("Thread interrupted");
}

//The next two blocks average the results of the different threads into 1 array
double[] meanArray = new double[vertLimit];
double[] meanError = new double[vertLimit];
double[] fixProbArray = new double[vertLimit];
double[] fixProbError = new double[vertLimit];

for(int x=0;x<vertLimit;x++){
for(Investigator i:invArray){
meanArray[x] += i.getMeanArray()[x];
meanError[x] += Math.pow(i.getMeanError()[x], 2);
fixProbArray[x] += i.getFixProbArray()[x];
fixProbError[x] += Math.pow(i.getFixProbError()[x], 2);
}
meanArray[x] = meanArray[x]/numThreads;
fixProbArray[x] = fixProbArray[x]/numThreads;

meanError[x] = Math.sqrt(meanError[x]);
fixProbError[x] = Math.sqrt(fixProbError[x]);
}

long endTime = System.nanoTime();

//The remaining code is for printing and producing graphs of the results

以及 Investigator 类,其重要部分如下所示:

public class Investigator implements Runnable{

public Investigator(int vertLimit,int iterations,int graphNumber,Boolean isDirected,int mutantFitness,int... vertFloor){
//Constructor just initialises all the class variables passed in
}

public void run(){
GraphGenerator g = new GraphGenerator();
Statistics stats = new Statistics();

//The outer loop iterates through graphs with increasing number of vertices, this is the problematic loop that exits too early
for(int x = vertFloor>2?vertFloor:2; x < vertLimit; x++){

System.out.println("Current vertex amount: " + x);
double[] currentMean = new double[graphNumber];
double[] currentMeanErr = new double[graphNumber];
double[] currentFixProb = new double[graphNumber];
double[] currentFixProbErr = new double[graphNumber];

//This loop generates the required number of graphs of the given vertex number and performs a simulation on each one
for(int y=0;y<graphNumber;y++){

Simulator s = new Simulator();
matrix = g.randomGraph(x, isDirected, mutantFitness);

s.moranSimulation(iterations, matrix);

currentMean[y] = stats.freqMean(s.getFixationTimes());
currentMeanErr[y] = stats.freqStandError(s.getFixationTimes());
currentFixProb[y] = s.getFixationProb();
currentFixProbErr[y] = stats.binomialStandardError(s.getFixationProb(), iterations);
}

meanArray[x] = Arrays.stream(currentMean).sum()/currentMean.length;
meanError[x] = Math.sqrt(Arrays.stream(currentMeanErr).map(i -> i*i).sum());

fixProbArray[x] = Arrays.stream(currentFixProb).sum()/currentFixProb.length;
fixProbError[x] = Math.sqrt(Arrays.stream(currentFixProbErr).map(i -> i*i).sum());;
}
}

//A number of getter methods also provided here
}

问题

我已经放入了一些打印语句来弄清楚发生了什么,并且出于某种原因,当我将 isDirected 设置为 true 时,线程在 x 达到 vertLimit 之前完成(我已经检查过确实是我指定的值)。我已经尝试手动使用我的 GraphGenerator.randomGraph() 方法来生成有向图,它给出了正确的输出,并测试了 Simulator.moranSimulation() ,当手动调用时,它也适用于有向图,并且我没有收到 catch block 捕获的线程中断,所以这也不是问题。

对于同一组参数,线程看似随机地在不同阶段完成,有时它们停止时都处于相同的 x 值,有时某些线程会比其他线程走得更远,但每次运行情况都会发生变化。

我完全被难住了,非常感谢一些帮助,谢谢。

最佳答案

当任务由 ExecutorService 运行时,如果抛出未处理的异常,它们有时可能会提前结束。

每次您调用 .submit(Runnable).submit(Callable)你会得到一个 Future代表任务最终完成的对象返回。 Future 对象有一个 .get()任务完成后将返回任务结果的方法。调用此方法将阻塞,直到结果可用。另外,如果任务抛出一个异常,而任务代码未以其他方式处理该异常,则对 .get() 的调用将抛出一个 ExecutionException,它将包装实际抛出的异常。

如果您的代码由于未处理的异常而过早退出,请在提交要执行的任务时(在您提交了所有您想要执行的任务之后)获得的每个 Future 对象上调用 .get() ,并捕获恰好抛出的任何 ExecutionExceptions 以找出实际的潜在问题是什么。

关于java - 线程对于某些值提前停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28818494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com