Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java CompletableFuture supply Async有时在主线程上运行_Java_Multithreading_Completable Future - Fatal编程技术网

Java CompletableFuture supply Async有时在主线程上运行

Java CompletableFuture supply Async有时在主线程上运行,java,multithreading,completable-future,Java,Multithreading,Completable Future,我一直在试验Java8 CompletableFutures。我的理解是,调用CompletableFuture.supplyAsync(供应商-供应商,执行者-执行者)将始终在传入执行者提供的线程中运行作业,但我注意到,当我传递给它的供应商相当“简单”时,它有时会在主线程上运行。我的测试代码是: import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; import j

我一直在试验Java8 CompletableFutures。我的理解是,调用CompletableFuture.supplyAsync(供应商-供应商,执行者-执行者)将始终在传入执行者提供的线程中运行作业,但我注意到,当我传递给它的供应商相当“简单”时,它有时会在主线程上运行。我的测试代码是:

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

public class SyncTest {

    public static void main(String[] args) {
        ExecutorService pool = Executors.newFixedThreadPool(5);

        CompletableFuture<?>[] cfs = new CompletableFuture<?>[10];
        AtomicInteger mainCount = new AtomicInteger();
        for (int i = 0; i < cfs.length; i++) {
            int index = i;
            CompletableFuture<Integer> cf = 
CompletableFuture.supplyAsync(() -> {
                return index;
            }, pool).thenApply(j -> {
                if (Thread.currentThread().getName().equals("main")) {    
                    mainCount.incrementAndGet();
                }

                System.out.println(Thread.currentThread().getName() + ": " + index + ": doing a heavy computation");
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return j;
            });
            cfs[i] = cf;
        }

        System.out.println(Thread.currentThread().getName() + " doing other stuff");
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        CompletableFuture.allOf(cfs).join();
        pool.shutdown();
        System.out.println("Jobs on main: " + mainCount.get());
    }
}
我知道这是一个相当简单的例子,还有其他一些方法可以实现CompletableFutures,比如SupplySync和completedFuture。我更好奇的是,其中一些任务是如何在主线程上执行的。

打印“正在进行大量计算”的任务通过
然后apply调用,这意味着您没有指定运行此任务的执行器,并且系统可以自由使用任何执行器,包括当前线程


如果要在预定义的执行器上执行此作业,请使用
然后使用applyasync
,无论是否使用第二个参数-executor。

检查此问题的答案:可能重复
main: 0: doing a heavy computation
main: 1: doing a heavy computation
pool-1-thread-3: 2: doing a heavy computation
pool-1-thread-4: 3: doing a heavy computation
main: 4: doing a heavy computation
main doing other stuff
pool-1-thread-4: 9: doing a heavy computation
pool-1-thread-5: 8: doing a heavy computation
pool-1-thread-2: 7: doing a heavy computation
pool-1-thread-1: 6: doing a heavy computation
pool-1-thread-3: 5: doing a heavy computation
Jobs on main: 3