executorservice

What are the advantages of using an ExecutorService?

拜拜、爱过 提交于 2019-11-26 08:03:12
问题 What is the advantage of using ExecutorService over running threads passing a Runnable into the Thread constructor? 回答1: ExecutorService abstracts away many of the complexities associated with the lower-level abstractions like raw Thread . It provides mechanisms for safely starting, closing down, submitting, executing, and blocking on the successful or abrupt termination of tasks (expressed as Runnable or Callable ). From JCiP, Section 6.2, straight from the horse's mouth: Executor may be a

Is ExecutorService (specifically ThreadPoolExecutor) thread safe?

旧城冷巷雨未停 提交于 2019-11-26 06:37:35
问题 Does the ExecutorService guarantee thread safety ? I\'ll be submitting jobs from different threads to the same ThreadPoolExecutor, do I have to synchronize access to the executor before interacting/submitting tasks? 回答1: It's true, the JDK classes in question don't seem to make an explicit guarantee of thread-safe task submission. However, in practice, all ExecutorService implementations in the library are indeed thread-safe in this way. I think it's reasonable to depend on this. Since all

How to shutdown an ExecutorService?

懵懂的女人 提交于 2019-11-26 06:30:37
问题 Whenever I call shutdownNow() or shutdown() it doesn\'t shut down. I read of a few threads where it said that shutting down is not guaranteed - can someone provide me a good way of doing it? 回答1: 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

Turning an ExecutorService to daemon in Java

自古美人都是妖i 提交于 2019-11-26 06:29:52
问题 I am using an ExecutoreService in Java 1.6, started simply by ExecutorService pool = Executors.newFixedThreadPool(THREADS). When my main thread is finished (along with all the tasks processed by the thread pool), this pool will prevent my program from shutting down until I explicitly call pool.shutdown(); Can I avoid having to call this by somehow turning the internal thread managing used by this pool into a deamon thread? Or am I missing something here. 回答1: Probably simplest and preferred

Executors.newCachedThreadPool() versus Executors.newFixedThreadPool()

試著忘記壹切 提交于 2019-11-26 05:57:09
问题 newCachedThreadPool() versus newFixedThreadPool() When should I use one or the other? Which strategy is better in terms of resource utilization? 回答1: I think the docs explain the difference and usage of these two functions pretty well: newFixedThreadPool Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue. At any point, at most nThreads threads will be active processing tasks. If additional tasks are submitted when all threads are active, they

Handling exceptions from Java ExecutorService tasks

為{幸葍}努か 提交于 2019-11-26 05:42:08
I'm trying to use Java's ThreadPoolExecutor class to run a large number of heavy weight tasks with a fixed number of threads. Each of the tasks has many places during which it may fail due to exceptions. I've subclassed ThreadPoolExecutor and I've overridden the afterExecute method which is supposed to provide any uncaught exceptions encountered while running a task. However, I can't seem to make it work. For example: public class ThreadPoolErrors extends ThreadPoolExecutor { public ThreadPoolErrors() { super( 1, // core threads 1, // max threads 1, // timeout TimeUnit.MINUTES, // timeout

How to properly use Java Executor?

旧城冷巷雨未停 提交于 2019-11-26 04:44:36
问题 I\'ve used Java Executors in my multi-threading apps, but I can\'t seem to figure out when is the best to use each of the following ways: 1. ExecutorService executor=Executors.newFixedThreadPool(50); executor.execute(new A_Runner(... some parameter ...)); executor.shutdown(); while (!executor.isTerminated()) { Thread.sleep(100); } 2. int Page_Count=200; ExecutorService executor=Executors.newFixedThreadPool(50); doneSignal=new CountDownLatch(Page_Count); for (int i=0;i<Page_Count;i++) executor

How to properly shutdown java ExecutorService

别说谁变了你拦得住时间么 提交于 2019-11-26 01:47:38
问题 I have a simple java ExecutorService that runs some task objects (implements Callable ). ExecutorService exec = Executors.newSingleThreadExecutor(); List<CallableTask> tasks = new ArrayList<>(); // ... create some tasks for (CallableTask task : tasks) { Future future = exec.submit(task); result = (String) future.get(timeout, TimeUnit.SECONDS); // TASKS load some classes and invoke their methods (they may create additional threads) // ... catch interruptions and timeouts } exec.shutdownNow();

Handling exceptions from Java ExecutorService tasks

橙三吉。 提交于 2019-11-26 01:40:54
问题 I\'m trying to use Java\'s ThreadPoolExecutor class to run a large number of heavy weight tasks with a fixed number of threads. Each of the tasks has many places during which it may fail due to exceptions. I\'ve subclassed ThreadPoolExecutor and I\'ve overridden the afterExecute method which is supposed to provide any uncaught exceptions encountered while running a task. However, I can\'t seem to make it work. For example: public class ThreadPoolErrors extends ThreadPoolExecutor { public

ExecutorService, how to wait for all tasks to finish

冷暖自知 提交于 2019-11-25 23:45:50
问题 What is the simplest way to to wait for all tasks of ExecutorService to finish? My task is primarily computational, so I just want to run a large number of jobs - one on each core. Right now my setup looks like this: ExecutorService es = Executors.newFixedThreadPool(2); for (DataTable singleTable : uniquePhrases) { es.execute(new ComputeDTask(singleTable)); } try{ es.wait(); } catch (InterruptedException e){ e.printStackTrace(); } ComputeDTask implements runnable. This appears to execute the