ExecutorCompletionService? Why do need one if we have invokeAll?

Using a ExecutorCompletionService.poll/take, you are receiving the Futures as they finish, in completion order (more or less). Using ExecutorService.invokeAll, you do not have this power; you either block until are all completed, or you specify a timeout after which the incomplete are cancelled. static class SleepingCallable implements Callable<String> { final String name; final long period; … Read more

Controlling Task execution order with ExecutorService

I write own Executor that warrants task ordering for tasks with same key. It uses map of queues for order tasks with same key. Each keyed task execute next task with the same key. This solution don’t handle RejectedExecutionException or other exceptions from delegated Executor! So delegated Executor should be “unlimited”. import java.util.HashMap; import java.util.LinkedList; … Read more

Whether to use invokeAll or submit – java Executor service

Option 1 : You are submitting the tasks to ExecutorService and you are not waiting for the completion of all tasks, which have been submitted to ExecutorService Option 2 : You are waiting for completion of all tasks, which have been submitted to ExecutorService. What should be the preferred way? Depending on application requirement, either … Read more

How to pause/resume all threads in an ExecutorService in Java?

To answer my own question, I found an example of a PausableThreadPoolExecutor in the javadocs of ThreadPoolExecutor itself. Here is my version using Guava’s Monitors: import com.google.common.util.concurrent.Monitor; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.ThreadFactory; public class PausableExecutor extends ScheduledThreadPoolExecutor { private boolean isPaused; private final Monitor monitor = new Monitor(); private final Monitor.Guard paused = new Monitor.Guard(monitor) { … Read more