threadpoolexecutor

How to get the ThreadPoolExecutor to increase threads to max before queueing?

ぐ巨炮叔叔 提交于 2019-11-26 14:06:29
I've been frustrated for some time with the default behavior of ThreadPoolExecutor which backs the ExecutorService thread-pools that so many of us use. To quote from the Javadocs: If there are more than corePoolSize but less than maximumPoolSize threads running, a new thread will be created only if the queue is full . What this means is that if you define a thread pool with the following code, it will never start the 2nd thread because the LinkedBlockingQueue is unbounded. ExecutorService threadPool = new ThreadPoolExecutor(1 /*core*/, 50 /*max*/, 60 /*timeout*/, TimeUnit.SECONDS, new

How to wait for all tasks in an ThreadPoolExecutor to finish without shutting down the Executor?

荒凉一梦 提交于 2019-11-26 12:55:00
问题 I can\'t use shutdown() and awaitTermination() because it is possible new tasks will be added to the ThreadPoolExecutor while it is waiting. So I\'m looking for a way to wait until the ThreadPoolExecutor has emptied it\'s queue and finished all of it\'s tasks without stopping new tasks from being added before that point. If it makes any difference, this is for Android. Thanks Update : Many weeks later after revisiting this, I discovered that a modified CountDownLatch worked better for me in

Core pool size vs maximum pool size in ThreadPoolExecutor

不打扰是莪最后的温柔 提交于 2019-11-26 11:59:42
问题 What exactly is difference between core pool size and maximum pool size when we talk in terms of ThreadPoolExecutor? Can it be explained with the help of an example? 回答1: From this blog post: Take this example. Starting thread pool size is 1, core pool size is 5, max pool size is 10 and the queue is 100. As requests come in, threads will be created up to 5 and then tasks will be added to the queue until it reaches 100. When the queue is full new threads will be created up to maxPoolSize .

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

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