1

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

收到错误

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

成长种子SWRun

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

2 回答 2

3

你最好检查以下问题:</p>

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

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

于 2015-06-23T16:05:11.300 回答
2

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

于 2015-06-23T15:53:40.077 回答