How to stop all runnable thread in java executor class?

The shutDown() method simply prevents additional tasks from being scheduled. Instead, you could call shutDownNow() and check for thread interruption in your Runnable. // in your Runnable… if (Thread.interrupted()) { // Executor has probably asked us to stop } An example, based on your code, might be: final ExecutorService executor = Executors.newFixedThreadPool(1); executor.submit(new Runnable() { … Read more

Java: ExecutorService that blocks on submission after a certain queue size [duplicate]

I have done this same thing. The trick is to create a BlockingQueue where the offer() method is really a put(). (you can use whatever base BlockingQueue impl you want). public class LimitedQueue<E> extends LinkedBlockingQueue<E> { public LimitedQueue(int maxSize) { super(maxSize); } @Override public boolean offer(E e) { // turn offer() and add() into a … Read more

ThreadPoolExecutor Block When its Queue Is Full?

In some very narrow circumstances, you can implement a java.util.concurrent.RejectedExecutionHandler that does what you need. RejectedExecutionHandler block = new RejectedExecutionHandler() { rejectedExecution(Runnable r, ThreadPoolExecutor executor) { executor.getQueue().put( r ); } }; ThreadPoolExecutor pool = new … pool.setRejectedExecutionHandler(block); Now. This is a very bad idea for the following reasons It’s prone to deadlock because all the … Read more

FixedThreadPool vs CachedThreadPool: the lesser of two evils

A CachedThreadPool seems appropriate for your situation as there are no negative consequence to using one for long running threads directly. The comment in the java doc about CachedThreadPools being suitable for short tasks merely suggest that they are particularly appropriate for such cases, not that they cannot be used for long running tasks. The … Read more

How to shutdown an ExecutorService?

The typical pattern is: executorService.shutdownNow(); executorService.awaitTermination(); When calling shutdownNow, the executor will (generally) try to interrupt the threads that it manages. To make the shutdown graceful, you need to catch the interrupted exception in the threads or check the interrupted status. If you don’t your threads will run forever and your executor will never be … Read more

How to use invokeAll() to let all thread pool do their task?

The way an ExecutorService works is that when you call invokeAll it waits for all tasks to complete: Executes the given tasks, returning a list of Futures holding their status and results when all complete. Future.isDone() is true for each element of the returned list. Note that a completed task could have terminated either normally … Read more

ThreadPoolExecutor Block When Queue Is Full?

In some very narrow circumstances, you can implement a java.util.concurrent.RejectedExecutionHandler that does what you need. RejectedExecutionHandler block = new RejectedExecutionHandler() { rejectedExecution(Runnable r, ThreadPoolExecutor executor) { executor.getQueue().put( r ); } }; ThreadPoolExecutor pool = new … pool.setRejectedExecutionHandler(block); Now. This is a very bad idea for the following reasons It’s prone to deadlock because all the … Read more