asyncio: Is it possible to cancel a future been run by an Executor?

In this case, there is no way to cancel the Future once it has actually started running, because you’re relying on the behavior of concurrent.futures.Future, and its docs state the following: cancel() Attempt to cancel the call. If the call is currently being executed and cannot be cancelled then the method will return False, otherwise … Read more

Unhandled exceptions with Java scheduled executors

The Javadoc of both scheduleAtFixedRate and scheduleWithFixedDelay says “If any execution of the task encounters an exception, subsequent executions are suppressed.” I don’t find that to be exactly crystal clear, but it seems to be saying that if your run method throws any kind of exception, then the scheduler will effectively drop that task. Any … Read more

How to properly catch RuntimeExceptions from Executors?

The clean workaround is to use ExecutorService.submit() instead of execute(). This returns you a Future which you can use to retrieve the result or exception of the task: ExecutorService executor = Executors.newSingleThreadExecutor(); Runnable task = new Runnable() { public void run() { throw new RuntimeException(“foo”); } }; Future<?> future = executor.submit(task); try { future.get(); } … Read more

Elegantly implementing queue length indicators to ExecutorServices

There is a more direct way: ThreadPoolExecutor executor = new ThreadPoolExecutor( 1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>() ); // add jobs // … int size = executor.getQueue().size(); This is directly copied from Executors.newSingleThreadExecutor in JDK 1.6. The LinkedBlockingQueue that is passed to the constructor is actually the very object that you will get back from … Read more

Detailed difference between Java8 ForkJoinPool and Executors.newWorkStealingPool?

Work stealing is a technique used by modern thread-pools in order to decrease contention on the work queue. A classical threadpool has one queue, and each thread-pool-thread locks the queue, dequeue a task and then unlocks the queue. If the tasks are short and there are many of them, there is a lot of contention … Read more

Java Executors: how can I set task priority?

Currently the only concrete implementations of the Executor interface are the ThreadPoolExecutor and the ScheduledThreadpoolExecutor Instead of using the utility / factory class Executors, you should create an instance using a constructor. You can pass a BlockingQueue to the constructors of the ThreadPoolExecutor. One of the implementations of the BlockingQueue, the PriorityBlockingQueue lets you pass … 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