I am working on the Executors in java to concurrently run more threads at a time. I have a set of Runnable Objects and i assign it to the Exceutors.The Executor is working fine and every thing is fine.But after all the tasks are executed in the pool ,the java program is not terminated,i think the Executor takes some time to kill the threads.please anyone help me to reduce the time taken by the executor after executing all tasks.
相关问题
- Java- Why this program not throwing concurrent Mod
- java - stealing bits from references
- why doesn't this erlang code work?
- Pattern for Java ConcurrentHashMap of Sets
- How to Stop a Running a Program Using Other Java P
相关文章
- Pattern for Java ConcurrentHashMap of Sets
- How to Stop a Running a Program Using Other Java P
- Blocking Locks versus Non-Blocking Locks
- ConcurrentHashMap returns a weakly consistent iter
- Is parallel programming == multithread programming
- Java: reference escape
- How to solve the producer-consumer using semaphore
- What are the main uses of yield(), and how does it
executor.shutdown() with awaitTermination(timeout) does not kill threads. (ie), if your runnable task is inside a polling loop, it does not kill the task. All it does is to interrupt its runnable tasks when the timeout is reached. So, in the code for your runnable class, if you wait on some condition, you may want to change the condition as,
This ensures that the task stops when the thread is interrupted as the while loop terminates. Alternately, you might want to catch the interrupted exception and set flag=false in the catch block to terminate the thread.
You might also want to use a profiler to examine why some threads have not proceeded to completion.
The ExecutorService class has 2 methods just for this: shutdown() and shutdownNow().
After using the shutdown() method, you can call awaitTermination() to block until all of the started tasks have completed. You can even provide a timeout to prevent waiting forever.
You might want to click on some of these links that I'm providing. They go straight to the docs where you can readup on this stuff yourself.