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

How to fix java.util.concurrent.CompletionException: java.lang.StackOverflowError

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

收到错误:

Exception in thread "main" java.util.concurrent.CompletionException: java.lang.WhosebugError
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.WhosebugError
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]);
        }
    }
}

您的 growSeedsSW 方法是递归的,它似乎耗尽了最大堆栈大小。你能以迭代的方式重写它吗? 否则,您可以尝试使用 Xss 标志增大堆栈大小,如 How to increase the Java stack size?.

中所述

你最好检查以下问题:

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

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