如何修复java.util.concurrent.CompletionException:java.lang.StackOverflower错误

如何修复java.util.concurrent.CompletionException:java.lang.StackOverflower错误,java,multithreading,thread-safety,completable-future,Java,Multithreading,Thread Safety,Completable Future,我正在编写一个递归代码,根据像素值的相似性来绘制对象的轮廓。正如您在下面的代码中看到的,我正在使用四个线程异步工作,但在运行时,我收到下面发布的错误,我不知道 如何修复它 收到错误: Exception in thread "main" java.util.concurrent.CompletionException: java.lang.StackOverflowError at java.util.concurrent.CompletableFuture.encodeThrowable(Un

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

收到错误:

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();
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]);
        }
    }
}
代码

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();
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]);
        }
    }
}
成长种子运行

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();
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]);
        }
    }
}

您的
growsedssw
方法是递归的,它似乎耗尽了最大堆栈大小。你能用迭代的方式重写它吗?
否则,您可以尝试使用Xss标志使堆栈大小变大,如中所述。

您的
growtheedssw
方法是递归的,它似乎耗尽了最大堆栈大小。你能用迭代的方式重写它吗?
否则,您可以尝试使用Xss标志增大堆栈大小,如中所述。

您最好检查以下问题:

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


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

您最好检查以下问题:

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


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

您能告诉我如何知道当前堆栈的大小吗?有没有办法通过eclipse选项来实现这一点?请参阅。然后您可以在JRE属性或Eclipse中的运行配置JVM属性中设置-Xss,是的。您能告诉我如何知道当前堆栈大小吗?有没有办法通过eclipse选项来实现这一点?请参阅。然后,您可以在JRE属性或Eclipse中的运行配置JVM属性中设置-Xss,是的。