How to stop a Callable submitted to ExecutorService?

前端 未结 2 1736
鱼传尺愫
鱼传尺愫 2021-02-05 23:01

I\'m trying to implement a sample application to test Callable and ExecutorService interfaces.

In my app I have declared:

Execu         


        
2条回答
  •  离开以前
    2021-02-05 23:36

    This is how I'd do it with a FixedThreadPool, hope it's of some help.

        ExecutorService pool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
    
        List> results = new ArrayList<>();
    
        for (int i = 0; i < numberOfJobs; i++) {
            MyCallableJob job = new MyCallableJob (...);
            results.add(pool.submit(job));
        }
    
        for (Future result : results) {
            try { result.get(); }
            catch (InterruptedException | ExecutionException ignorable) { }
        }
    
        pool.shutdown();
    

提交回复
热议问题