gpt4 book ai didi

java - 如何修复 java.util.concurrent.CompletionException : java. lang.StackOverflowError

转载 作者:行者123 更新时间:2023-11-29 07:40:12 25 4
gpt4 key购买 nike

我正在编写一个递归代码,根据像素值相似度来绘制对象轮廓。正如您在下面的代码中看到的,我正在使用四个异步工作的线程,但在运行时我收到以下发布的错误,我不知道如何修复它。

收到错误:

Exception in thread "main" java.util.concurrent.CompletionException: java.lang.StackOverflowError
at java.util.concurrent.CompletableFuture.encodeThrowable(Unknown Source)
at java.util.concurrent.CompletableFuture.completeThrowable(Unknown Source)
at java.util.concurrent.CompletableFuture$AsyncRun.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.StackOverflowError
at java.util.ArrayList.contains(Unknown Source)
at java.util.Collections$SynchronizedCollection.contains(Unknown Source)
at com.example.seedgrowtest.SeedGrowing$GrowSeedSWRun.growSeedsSW(SeedGrowing.java:234)
at com.example.seedgrowtest.SeedGrowing$GrowSeedSWRun.growSeedsSW(SeedGrowing.java:246)
at com.example.seedgrowtest.SeedGrowing$GrowSeedSWRun.growSeedsSW(SeedGrowing.java:246)

代码:

this.growSeedFutureList = CompletableFuture.runAsync(new GrowSeedSERun(this.saliencyMat, this.seedXY, this.seedVal), this.growSeedExecutor);
this.growSeedFutureList = CompletableFuture.runAsync(new GrowSeedNWRun(this.saliencyMat, this.seedXY, this.seedVal), this.growSeedExecutor);
this.growSeedFutureList = CompletableFuture.runAsync(new GrowSeedNERun(this.saliencyMat, this.seedXY, this.seedVal), this.growSeedExecutor);
this.growSeedFutureList = CompletableFuture.runAsync(new GrowSeedSWRun(this.saliencyMat, this.seedXY, this.seedVal), this.growSeedExecutor);
CompletableFuture.allOf(this.growSeedFutureList).join();

GrowSeedSWRun:

private class GrowSeedSWRun implements Runnable {

private Mat saliencyMat = null;
private double seedVal;
private Point seedXY = null;

public GrowSeedSWRun(Mat saliencyMat, Point seedXY, double seedVal) {
// TODO Auto-generated constructor stub
this.saliencyMat = saliencyMat;
this.seedXY = seedXY;
this.seedVal = seedVal;
}
public void run() {
// TODO Auto-generated method stub
this.growSeedsSW(this.saliencyMat, this.seedXY, this.seedVal);
}

private void growSeedsSW(Mat saliencyMat, Point seedXY, Double seedVal) {
// TODO Auto-generated method stub
int origX = (int) seedXY.x;
int origY = (int) seedXY.y;

if ( ((saliencyMat.get(origY, --origX) != null)) && ( withinRange(saliencyMat.get(origY, origX)[0]) )) {

synchronized (grownSeedXYList) {//line number 234
if (!grownSeedXYList.contains(new Point(origX, origY))) {

"+saliencyMat.get(origY, origX)[0]);

grownSeedXYList.add(new Point(origX, origY));
} else {
Log.D(TAG, "growSeedsSW", "point: "+ new Point(origX, origY)+" contained in the list");
}
}

this.growSeedsSW(saliencyMat, new Point(origX, origY), saliencyMat.get(origY, origX)[0]);//line number 246

//check if this != null, because it might be the on the edge of the image
} else if ( (saliencyMat.get(++origY, (int) this.seedXY.x) != null) && ( withinRange(saliencyMat.get(origY, (int) this.seedXY.x)[0]) )) {
origX = (int) this.seedXY.x;

synchronized (grownSeedXYList) {
if (!grownSeedXYList.contains(new Point(origX, origY))) {

"+saliencyMat.get(origY, origX)[0]);

grownSeedXYList.add(new Point(origX, origY));
}else {
Log.D(TAG, "growSeedsSW", "point: "+ new Point(origX, origY)+" contained in the list");
}
}
this.growSeedsSW(saliencyMat, new Point(origX, origY), saliencyMat.get(origY, origX)[0]);
}
}
}

最佳答案

你最好检查以下问题:

1.你的算法递归太深。超出堆栈大小。

2.你的算法存在无限递归问题。

关于java - 如何修复 java.util.concurrent.CompletionException : java. lang.StackOverflowError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31007548/

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