简体   繁体   中英

CompletableFuture.runAsync not finishing execution

I'm attempting to use CompletableFuture to write a large number of pixels on a JavaFX canvas, and my method does not seem to be completing its task despite no exceptions being thrown.

I'm new to asynchronous programming and am not sure where I should begin, but I thought this might work from what info I've gathered.

I'm attempting to use:

CompletableFuture.runAsync(() -> {
                mandelbrot.increaseScale();
                drawMandelbrot();
            });

to call:

private void drawMandelbrot() {
    //RENDER

    gc.clearRect(0,0,UISpecs.CANVAS_SIZE.getValue(),UISpecs.CANVAS_SIZE.getValue()); // clears canvas of any previously set pixels
    gc.setFill(Color.BLACK); // set canvas background to be black
    gc.fillRect(0,0,UISpecs.CANVAS_SIZE.getValue(),UISpecs.CANVAS_SIZE.getValue()); // set canvas background

    for(double a = -2; a <= 2; a+=.001) {
        for(double b = -2; b <= 2; b+=.001) {
            c = new Complex(a,b);
            col = pickColor(doIterations(c));
            p.setColor(mandelbrot.getX(a), mandelbrot.getY(b), col);
        }
    }
} // drawMandelbrot()

My complex(a,b) and pickColor(i) methods are quite trivial, both do very small and simple operations. So I believe my nested loops are what is causing my trouble.

Any guidance or input is greatly appreciated.

CompletableFuture is an implementation of the Future interface and you want your pixels to be rendered onto the canvas asynchronously but you are not waiting for the future to complete its computation ,

CompletableFuture<Void> completableFuture =CompletableFuture.runAsync(() -> {
                mandelbrot.increaseScale();
                drawMandelbrot();
            });
completableFuture.get();

The documentation for CompletableFuture.get() puts it clearly : Waits for the async computation to finish and then returns

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM