首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何修复java.util.concurrent.CompletionException: java.lang.StackOverflowError

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

Stack Overflow用户
提问于 2015-06-23 15:48:47
回答 2查看 17.7K关注 0票数 1

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

错误接收到

代码语言:javascript
复制
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)

码

代码语言:javascript
复制
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

代码语言:javascript
复制
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]);
        }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-06-23 15:53:40

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

票数 2
EN

Stack Overflow用户

发布于 2015-06-23 16:05:11

你最好检查下列问题:

1.您的算法递归太深了。在堆栈大小之外。

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31007548

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档