执行者没有 Return 10 个未来对象

Executor Does Not Return 10 Future Objects

我有一个线程池为 10 的执行程序服务,我预计我会得到 10 个相隔三秒的打印输出语句,但我只收到一个打印输出语句。我传递了 10 作为参数,所以我期望 10 个线程是 运行。如何检索 10 个未来对象?

public class Demo {
    private static final ExecutorService executor = Executors.newFixedThreadPool(10);

    public static void main (String[] args) throws ExecutionException, InterruptedException {

        ArrayList futureObjects = new ArrayList();

        Callable<Integer> task = () -> {
            try {
                TimeUnit.SECONDS.sleep(3);
                return 123;
            }
            catch (InterruptedException e) {
                throw new IllegalStateException("task interrupted", e);
            }
        };

        System.out.println("Before execution of threads");

        Future<Integer> future = executor.submit(task);

        Integer result = future.get();
        futureObjects.add(future.get());

        System.out.println("result: " + result);

        for(Object futures : futureObjects ){
            System.out.println("Futures in ArrayList: " + futures);
        }
    }

}

我得到的输出是:

线程执行前

结果:123

ArrayList 中的期货:123

已创建Executor会尝试在10个线程中并行执行任务,但每个提交的任务只会执行一次。

您实际上只添加了一个任务并提交给了线程池,因此执行并返回了一个任务。

您需要同时提交多个任务(使用下面的选项 1 或选项 2),以便您可以实际利用线程池(以保持线程忙碌)。

您可以查看以下代码的更新版本:

选项(1):ExecutorService-invokeAll():

private static final ExecutorService executor = Executors.newFixedThreadPool(10);

    public static void main (String[] args) throws ExecutionException, InterruptedException {

        ArrayList futureObjects = new ArrayList();

        Callable<Integer> task = () -> {
            try {
                TimeUnit.MILLISECONDS.sleep(100);
                return 123;
            }
            catch (InterruptedException e) {
                throw new IllegalStateException("task interrupted", e);
            }
        };

        List<Callable<Integer>> callables = new ArrayList<>();
        callables.add(task);
        callables.add(task);
        callables.add(task);
        callables.add(task);
        //Add other tasks

        System.out.println("Before execution of threads");

        List<Future<Integer>> futures = executor.invokeAll(callables);

        for(Future future : futures ){
            System.out.println("Futures in ArrayList: " + future.get());
        }
    }

选项(2):ExecutorService-提交():

private static final ExecutorService executor = Executors.newFixedThreadPool(10);

    public static void main (String[] args) throws ExecutionException, InterruptedException {

        ArrayList futureObjects = new ArrayList();

        Callable<Integer> task = () -> {
            try {
                TimeUnit.MILLISECONDS.sleep(100);
                return 123;
            }
            catch (InterruptedException e) {
                throw new IllegalStateException("task interrupted", e);
            }
        };

        List<Callable<Integer>> callables = new ArrayList<>();
        callables.add(task);
        callables.add(task);
        callables.add(task);
        callables.add(task);
        //Add other tasks

        List<Future<Integer>> futures = new ArrayList<>();
        System.out.println("Before execution of threads");

        for(Callable<Integer> callable : callables) {
            futures.add(executor.submit(callable));
        }

        for(Future future : futures ){
            System.out.println("Futures in ArrayList: " + future.get());
        }
    }

可以参考APIhere