Executor service missing few tasks

I have around 900 tasks. I have set threadpool count to be 50. I am running a loop and submitting every task to the executorService.

As soon as control comes out I call shutdown as below

for (Entry<String, String> CurrentJob : Tasks.entrySet()) {
            m_service.submit(new MyTask(CurrentJob.getValue(), CurrentJob.getKey()));
        }
          m_service.shutdown();

Each task takes around 1 sec on average.Now I have two questions

a) Almost all of them are doing the job(850) but around 50 are lost. I have put debugger at callable , but control is not even coming there for those specific task. Although if I put them individually they work. What logic of Executor service am I missing ?

b) I have put a timer around this code and according to javaDoc Shutdown should wait until all threads are done but my timer always says time taken zero.


The ExecutorService will not wait for previously submitted tasks to complete. From the JavaDocs:

https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html#shutdown--

This method does not wait for previously submitted tasks to complete execution. Use awaitTermination to do that.

So what may be happening is the ExecutorService shuts down, continues to finish the queued work, but the JVM exits because your Main thread terminates (leaving only Daemon threads, which will not keep the JVM alive)? Perhaps that's the problem?


To handle rejected tasks by executor service ThreadPoolExecutor provides RejectedHandler class.

Example ThreadPoolExecutor threadP = (ThreadPoolExecutor) Executors.newFixedThreadPool(3); threadP.setRejectedExecutionHandler(new RejectedHandler()); // add tasks

threadP.shutdown()

链接地址: http://www.djcxy.com/p/50188.html

上一篇: 单线程执行者无声地丢弃任务

下一篇: 执行器服务缺少一些任务