Executor Service和Main Thread不并行执行

时间:2018-06-08 04:18:26

标签: multithreading asynchronous executorservice

我要求主线程和Executor服务都应该并行执行。主线程不应该等待执行程序服务中的所有线程完成。它应该在执行程序服务执行其线程时继续执行。 示例主要方法

public static void main(String[] args) throws InterruptedException, ExecutionException {
        String mess = "test completed";
        //Start of ExecutorService - ThreadPool of size 500
        ExecutorService executorService = Executors.newFixedThreadPool(500);
        //Creating 500 tasks to execute
        List<AsyncTask> callables = new ArrayList<>();
        for(int i=1 ; i<= 500 ; i++){
            callables.add(new AsyncTask(i));
        }
        //Submitting all 500 tasks to execute
        List<Future<String>> futures = executorService.invokeAll(callables);
        for(Future<String> future : futures){
            System.out.println("future.get = " + future.get());
        }
        executorService.shutdown();
        //End of Executor service

        //Below code executes only when executor service is finished.
        //I want below code to execute in parallel with executor service
        //Means It should print this mess before executor service finishes
        System.out.println(mess);
    }

可调用任务

public class AsyncTask implements Callable<String>{
    private int i;
    public AsyncTask(int i) {
        this.i = i;
    }
    @Override
    public String call() throws Exception {
        return "Task " +i;
    }
}

OutPut:

future.get = Task 1
future.get = Task 2
future.get = Task 3
future.get = Task 4
.......continues till
future.get = Task 500
test completed

预期产出:

测试在某些异步任务完成期间或异步任务启动之前要打印的已完成消息

示例输出

future.get = Task 1
future.get = Task 2
test completed       // Both Main method and Executor service is running in parallel
future.get = Task 3
...........
future.get = Task 500

我之前没有使用Threads,我是否会错过什么?使用执行程序服务的整个想法是与主线程并行执行某些任务吗? 请建议我解决这个问题

1 个答案:

答案 0 :(得分:0)

ExecutorService.invokeAll将等待所有任务完成,因此在返回ExecutorService.invokeAll之后,所有代码都不会被执行。将服务执行代码放到另一个线程将简单地解决问题。

class RunService extends Thread {
     RunService () {
     }

     public void run() {
         //Start of ExecutorService - ThreadPool of size 500
         ExecutorService executorService = Executors.newFixedThreadPool(500);
         //Creating 500 tasks to execute
         List<AsyncTask> callables = new ArrayList<>();
         for(int i=1 ; i<= 500 ; i++){
             callables.add(new AsyncTask(i));
         }
         //Submitting all 500 tasks to execute
         List<Future<String>> futures = executorService.invokeAll(callables);
         for(Future<String> future : futures){
             System.out.println("future.get = " + future.get());
         }
         executorService.shutdown();
         //End of Executor service
     }
 }

public static void main(String[] args) throws InterruptedException, ExecutionException {
    String mess = "test completed";

    RunService rs = new RunService();
    rs.Start();

    //Below code executes only when executor service is finished.
    //I want below code to execute in parallel with executor service
    //Means It should print this mess before executor service finishes
    System.out.println(mess);
}