java - ExecutorService FixThreadPool 作为共享参数不并行运行

标签 java java-8 threadpool

我正在尝试在 Java 8 中使用固定线程池,只要它保持在同一函数内,它就可以完美工作。一旦我尝试将执行器共享为参数,它就永远不会并行运行。

这很好用:

```

public static void test2() {

ExecutorService executor =  Executors.newFixedThreadPool(2);
try {
    CompletionService<Integer> myCompletionService = 
               new ExecutorCompletionService<Integer>(executor);


    myCompletionService.submit(()-> {
        try {
            TimeUnit.SECONDS.sleep(5);
            return 123;
        }
        catch (InterruptedException e) {
            throw new IllegalStateException("task interrupted", e);
        }
    });


    CompletionService<Integer> myCompletionService2 = 
               new ExecutorCompletionService<Integer>(executor);
    myCompletionService2.submit(()-> {
        try {
            TimeUnit.SECONDS.sleep(5);
            return 654;
        }
        catch (InterruptedException e) {
            throw new IllegalStateException("task interrupted", e);
        }
    });

    Future<Integer> myFuture = myCompletionService.take();
    Integer x = myFuture.get();
    System.out.println("Result = " + x);

    Future<Integer> myFuture2 = myCompletionService2.take();
    Integer y = myFuture2.get();
    System.out.println("Result = " + y);

    executor.shutdown();
} catch (InterruptedException | ExecutionException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
}
```

但是一旦我将它们移动到三个函数中,例如:

```

static Integer t1(ExecutorService executor) throws InterruptedException, ExecutionException {
    CompletionService<Integer> myCompletionService = 
               new ExecutorCompletionService<Integer>(executor);


    myCompletionService.submit(()-> {
        try {
            TimeUnit.SECONDS.sleep(5);
            return 123;
        }
        catch (InterruptedException e) {
            throw new IllegalStateException("task interrupted", e);
        }
    });
    Future<Integer> myFuture = myCompletionService.take();
    return myFuture.get();
}

static Integer t2(ExecutorService executor) throws InterruptedException, ExecutionException {
    CompletionService<Integer> myCompletionService2 = 
               new ExecutorCompletionService<Integer>(executor);


    myCompletionService2.submit(()-> {
        try {
            TimeUnit.SECONDS.sleep(5);
            return 456;
        }
        catch (InterruptedException e) {
            throw new IllegalStateException("task interrupted", e);
        }
    });
    Future<Integer> myFuture2 = myCompletionService2.take();
    return myFuture2.get();
}

static void test3() {
    ExecutorService executor =  Executors.newFixedThreadPool(5);
    try {
        Integer x = t1(executor);
        Integer y = t2(executor);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    executor.shutdown();
}

``` 现在 test3 将需要 10 秒,我预计它与顶部的测试相同,如果并行运行,则应该需要 5 秒。,

最佳答案

在提交后的 t1 中,您调用 get() 并被阻止,因此仅当第一个任务完成时(5 秒后),您才从 t1 退出。

在第一个示例中,您提交了两个任务,以便它们开始在单独的线程中执行,然后仅调用 get() 来阻止并等待结果。

关于java - ExecutorService FixThreadPool 作为共享参数不并行运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53402069/

相关文章:

java - Apache 目录 API 拒绝将用户添加到 Active Directory "(UNWILLING_TO_PERFORM)"

java - ExecutorService 的线程和线程池命名

c++ - 让线程池的所有线程执行给定的函数

kotlin - kapt 中的 ArrayIndexOutOfBoundsException

java - 尝试在 Spark DataFrame 上使用 map

Java 8 HttpUrlConnection 无法解析 http 协议(protocol) 2 的响应

c# - 多线程:限制并发线程数

java - JTextPane 不断抛出 BadLocation

java - DeflatorInputStream 和 DeflatorOutputStream 不重构原始数据

java - 如何使按钮在 Java Swing 中正确突出显示或变白?