With a Java ExecutorService, how do I complete actively executing tasks but halt the processing of w
2015-03-27 15:15
369 查看
如何让正在执行的任务执行完,等待执行的任务全部取消?
I want something in between these two options.
I want to call a command that:
a. Completes the currently active task or tasks (like
b. Halts the processing of waiting tasks (like
I ran into this issue recently. There may be a more elegant approach, but my solution is to first call
then pull out the
used by the
call
it (or else drain it to another
storage). Finally, calling
the thread pool to finish what's currently on its plate.
For example:
This method would be implemented in the directing class wrapping the
the reference
It's important to note the following from the
Access to the task queue is intended primarily for debugging and monitoring. This queue may be in active use. Retrieving the task queue does not prevent queued tasks from executing.
This highlights the fact that additional tasks may be polled from the
you drain it. However, all
are thread-safe according to that interface's
documentation, so this shouldn't cause problems.
I want something in between these two options.
I want to call a command that:
a. Completes the currently active task or tasks (like
shutdown).
b. Halts the processing of waiting tasks (like
shutdownNow).
I ran into this issue recently. There may be a more elegant approach, but my solution is to first call
shutdown(),
then pull out the
BlockingQueuebeing
used by the
ThreadPoolExecutorand
call
clear()on
it (or else drain it to another
Collectionfor
storage). Finally, calling
awaitTermination()allows
the thread pool to finish what's currently on its plate.
For example:
public static void shutdownPool(boolean awaitTermination) throws InterruptedException { //call shutdown to prevent new tasks from being submitted executor.shutdown(); //get a reference to the Queue final BlockingQueue<Runnable> blockingQueue = executor.getQueue(); //clear the Queue blockingQueue.clear(); //or else copy its contents here with a while loop and remove() //wait for active tasks to be completed if (awaitTermination) { executor.awaitTermination(SHUTDOWN_TIMEOUT, TimeUnit.SECONDS); }}
This method would be implemented in the directing class wrapping the
ThreadPoolExecutorwith
the reference
executor.
It's important to note the following from the
ThreadPoolExecutor.getQueue()javadoc:
Access to the task queue is intended primarily for debugging and monitoring. This queue may be in active use. Retrieving the task queue does not prevent queued tasks from executing.
This highlights the fact that additional tasks may be polled from the
BlockingQueuewhile
you drain it. However, all
BlockingQueueimplementations
are thread-safe according to that interface's
documentation, so this shouldn't cause problems.
相关文章推荐
- In Delphi XE2 FireMonkey - How do i change the color of a button after pressing it
- How do I sort an array of hashes by a value in the hash?
- How do I make my GUI plot into an axes within the GUI figure rather than inside of a new figure in M
- How do I sort an array of hashes by a value in the hash?
- java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive
- Lotus Workflow 如何複製工作屬性 How do I copy the job of property
- How do you get the length of a string in jQuery?
- 【摘录】How do you change the drawing order of views in Android?
- How to fix the problem of executing gitenv.csh file failed
- The method getNotCompleteOrder(ResultBean) of type HomeServiceImpl must override a superclass method
- How do I get the name of an object's type in JavaScript
- How do I check if a type is a subtype OR the type of an object?
- java.lang.IllegalStateException: The content of the adapter has changed but ListView...的问题
- javax.xml.ws.webserviceexception class do not have a property of the name
- How is jstack being used to find the bottlenect of a java program
- 全网最详细使用Scrapy时遇到0: UserWarning: You do not have a working installation of the service_identity module: 'cannot import name 'opentype''. Please install it from ..的问题解决(图文详解)
- 解决java.lang.IllegalStateException: The content of the adapter has changed but ListView...的问题
- Everything you always wanted to know about the processing of customer exit variables, but …
- How to restore ASM based OCR after complete loss of the CRS diskgroup on Linux systems[ID 1062983.1]
- How do I list property names of a JavaBean?