执行者不会返回10个未来对象

时间:2016-11-04 23:56:13

标签: java multithreading executorservice

我有一个执行者服务,其线程池为10,我希望我能得到10个打印输出语句,相隔3秒,但我只收到一个打印输出语句。我传递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);
        }
    }

}

我得到的输出是:

执行线程之前

结果:1​​23

ArrayList中的期货:123

2 个答案:

答案 0 :(得分:2)

您实际上只添加了一项任务&amp;提交给Threadpool ,因为执行了哪一个任务&amp;返回。

您需要一起提交多个任务(使用下面的Option1或Option2),以便您可以实际使用Threadpool(以保持线程忙)。

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

选项(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-submit():

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

您可以参考API here

答案 1 :(得分:1)

Created Executor将尝试并行执行10个线程中的任务,但每个提交的任务只执行一次。