threadpoolexecutor

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

此生再无相见时 提交于 2019-11-27 06:31:59
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 this case. I'll keep the answer marked because it applies more to what I asked. If you are interested in

Core pool size vs maximum pool size in ThreadPoolExecutor

[亡魂溺海] 提交于 2019-11-27 06:26: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? user2568266 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 . Once all the threads are in use and the queue is full tasks will be rejected. As the queue reduces

线程池不允许使用Executors去创建,而是通过ThreadPoolExecutor的方式,这样的处理方式让写的同学更加明确线程池的运行规则,规避资源耗尽的风险。

送分小仙女□ 提交于 2019-11-27 05:41:51
前言:jdk1.7中java.util.concurrent.Executor线程池体系介绍 java.util.concurrent.Executor : 负责线程的使用与调度的根接口 |–ExecutorService:Executor的子接口,线程池的主要接口 |–ThreadPoolExecutor:ExecutorService的实现类  |–ScheduledExecutorService:ExecutorService的子接口,负责线程的调度 |–ScheduledThreadPoolExecutor:继承了ThreadPoolExecutor实现了ScheduledExecutorService 一、Executors的四种线程池 newCachedThreadPool 创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。线程池为无限大,当执行第二个任务时第一个任务已经完成,会复用执行第一个任务的线程,而不用每次新建线程。 创建方式: Executors.newCachedThreadPool(); newFixedThreadPool 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。定长线程池的大小最好根据系统资源进行设置,如Runtime.getRuntime()

Is having a single threadpool better design than multiple threadpools

隐身守侯 提交于 2019-11-27 02:42:04
问题 What are the advantages and disadvantages of having more than one threadpool in Java? I have seen code where there are multiple threadpools for different "types" of tasks, and I'm not sure whether its better design or just developers being lazy. One example is using a ScheduledThreadPoolExecutor for tasks that are executed periodically or have a timeout, and use another ThreadPoolExecutor for everything else. 回答1: The purpose of having separate dedicated threadpools is so that an activity

Specify task order execution in Java

試著忘記壹切 提交于 2019-11-27 02:12:19
问题 I've searched a lot but couldn't find any solution. I use java thread pool in such way: ExecutorService c = Executors.newFixedThreadPool(3); for (int i = 0; i < 10; ++i) { c.execute(new MyTask(i)); } In such way Tasks are executed in consequent order (as in queue). But I need change "select next task" strategy. So I want assign to each task specify priority (it isn't thread priority) and execute tasks correspond to these priorities. So when executor have finished another task it chooses next

Impossible to make a cached thread pool with a size limit?

五迷三道 提交于 2019-11-26 21:11:39
It seems to be impossible to make a cached thread pool with a limit to the number of threads that it can create. Here is how static Executors.newCachedThreadPool is implemented in the standard Java library: public static ExecutorService newCachedThreadPool() { return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); } So, using that template to go on to create a fixed sized cached thread pool: new ThreadPoolExecutor(0, 3, 60L, TimeUnit.SECONDS, new SynchronusQueue<Runable>()); Now if you use this and submit 3 tasks, everything will be fine.

Handling Exceptions for ThreadPoolExecutor

会有一股神秘感。 提交于 2019-11-26 20:24:57
问题 I have the following code snippet that basically scans through the list of task that needs to be executed and each task is then given to the executor for execution. The JobExecutor in turn creates another executor (for doing db stuff...reading and writing data to queue) and completes the task. JobExecutor returns a Future<Boolean> for the tasks submitted. When one of the task fails, I want to gracefully interrupt all the threads and shutdown the executor by catching all the exceptions. What

Dynamic Thread Pool

瘦欲@ 提交于 2019-11-26 18:33:53
问题 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

How to properly use Java Executor?

时光总嘲笑我的痴心妄想 提交于 2019-11-26 16:17:43
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.execute(new A_Runner(doneSignal, ... some parameter ...)); doneSignal.await(); executor.shutdown();

Executors.newCachedThreadPool() versus Executors.newFixedThreadPool()

风流意气都作罢 提交于 2019-11-26 15:38:12
newCachedThreadPool() versus newFixedThreadPool() When should I use one or the other? Which strategy is better in terms of resource utilization? bruno conde 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 will wait in the queue until a thread is available. If any thread terminates due to a failure