executorservice

What is the best way to handle an ExecutionException?

↘锁芯ラ 提交于 2019-11-27 17:42:40
I have a method that performs some task with a timeout. I use the ExecutorServer.submit() to get a Future object, and then I call future.get() with a timeout. This is working fine, but my question is the best way to handle checked exceptions that can be thrown by my task. The following code works, and preserves the checked exceptions, but it seems extremely clumsy and prone to break if the list of checked exceptions in the method signature changes. Any suggestions on how to fix this? I need to target Java 5, but I'd also be curious to know if there are good solutions in newer versions of Java.

ExecutorCompletionService? Why do need one if we have invokeAll?

给你一囗甜甜゛ 提交于 2019-11-27 17:23:58
If we use an ExecutorCompletionService we can submit a series of tasks as Callable s and get the result interacting with the CompletionService as a queue . But there is also the invokeAll of ExecutorService that accepts a Collection of tasks and we get a list of Future to retrieve the results. As far as I can tell, there is no benefit in using one or over the other (except that we avoid a for loop using an invokeAll that we would have to submit the tasks to the CompletionService ) and essentially they are the same idea with a slight difference. So why are there 2 different ways to submit a

Method call to Future.get() blocks. Is that really desirable?

不羁的心 提交于 2019-11-27 17:12:32
Please read the question carefully before marking this as duplicate. Below is the snippet of the pseudo code. My question is- Does the below code not defeat the very notion of parallel asynchronous processing? The reason I ask this is because in the below code the main thread would submit a task to be executed in a different thread. After submitting the task in the queue, it blocks on Future.get() method for the task to return the value. I would rather have the task executed in the main thread rather than submitting to a different thread and waiting for the results. What is that I gained by

Dynamic Thread Pool

牧云@^-^@ 提交于 2019-11-27 15:44:44
I have a long running process that listens to events and do some intense processing. Currently I use Executors.newFixedThreadPool(x) to throttle the number of jobs that runs concurrently, but depending of the time of the day, and other various factors, I would like to be able to dynamically increase or decrease the number of concurrent threads. If I decrease the number of concurrent threads, I want the current running jobs to finish nicely. Is there a Java library that let me control and dynamically increase or decrease the number of concurrent threads running in a Thread Pool ? (The class

Java - splitting work to multiple threads

南楼画角 提交于 2019-11-27 13:59:46
问题 I am posed with the following problem: I need to split work across multiple threads for perfomance reasons, but I am not sure what approach to take. Firstly, the task I would be supplying should return a value and take a parameter. Additionally, the main method (doing the main bit of work, not static main() ) is already running on separate thread and is invoked periodically. Also, this method must at some point WAIT for all threads to finish and then proceed. One approach (most obvious to me)

Future task of ExecutorService not truly cancelling

寵の児 提交于 2019-11-27 13:44:09
I push my Futures from a ExecutorService into a hash map. Later, I may call cancel on Futures from within the hash map. Although the result is true, I later hit breakpoints within the Callable procedure, as if the Future cancel() had no effect. I think it might be a case of two different references here (even though the reference IDs are listed as the same when breakpointing), but was wondering if some experts could chime in. Here's what the code looks like: ExecutorService taskExecutor = Executors.newCachedThreadPool(); Map <String, Future<Object>> results = new HashMap <String, Future<Object

Shutdown ExecutorService gracefully in webapp?

情到浓时终转凉″ 提交于 2019-11-27 13:33:08
In my webapp, I created a service that is using an ExecutorService with fixed size ThreadPool. I reuse the same ExecutorService during the whole application lifetime. private static ExecutorService pool = Executors.newFixedThreadPool(8); All is running in Tomcat which gives me the following error while shuting down: appears to have started a thread named [pool-1-thread-1] but has failed to stop it. This is very likely to create a memory leak. I do realize I need to shutdown the ExecutorService before shuting tomcat down. Soms SO thread already speak about this but I could not find a clean way

Reason for calling shutdown() on ExecutorService

元气小坏坏 提交于 2019-11-27 10:50:33
I was reading about it quite a bit in past couple hours, and I simply cannot see any reason ( valid reason) to call shutdown() on the ExecutorService , unless we have a humongous application which stores, dozens and dozens of different executor services that are not used for a long time. The only thing (from what I gather) the shutdown does, is do what a normal Thread does once it's done. When the normal Thread will finish the run method of the Runnable(or Callable), it will be passed to Garbage Collection to be collected. With Executor Service the threads will simply be put on hold, they will

What's the difference between Future and FutureTask in Java?

心已入冬 提交于 2019-11-27 10:44:07
问题 Since use ExecutorService can submit a Callable task and return a Future , why need to use FutureTask to wrap Callable task and use the method execute ? I feel they both do the same thing. 回答1: In fact you are correct. The two approaches are identical. You generally don't need to wrap them yourself. If you are, you're likely duplicating the code in AbstractExecutorService: /** * Returns a <tt>RunnableFuture</tt> for the given callable task. * * @param callable the callable task being wrapped

ExecutorService vs ThreadPoolExecutor using LinkedBlockingQueue

喜欢而已 提交于 2019-11-27 10:08:08
问题 I am working on a multithreaded project in which I need to spawn multiple threads to measure the end to end performance of my client code, as I'm doing Load and Performance testing. So I created the below code which is using ExecutorService . Below is the code with ExecutorService : public class MultithreadingExample { public static void main(String[] args) throws InterruptedException { ExecutorService executor = Executors.newFixedThreadPool(20); for (int i = 0; i < 100; i++) { executor