Questions tagged [threadpoolexecutor]

In Java, an ExecutorService that executes each submitted task using one of possibly several pooled threads.

In Java, an ExecutorService that executes each submitted task using one of possibly several pooled threads.

Documentation

1083 questions
245
votes
13 answers

Handling exceptions from Java ExecutorService tasks

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…
Tom
  • 18,685
  • 15
  • 71
  • 81
195
votes
8 answers

Executors.newCachedThreadPool() versus Executors.newFixedThreadPool()

newCachedThreadPool() versus newFixedThreadPool() When should I use one or the other? Which strategy is better in terms of resource utilization?
hakish
  • 3,990
  • 7
  • 39
  • 56
165
votes
12 answers

Core pool size vs maximum pool size in ThreadPoolExecutor

What exactly is the 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
  • 3,673
  • 4
  • 15
  • 8
143
votes
14 answers

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

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…
118
votes
10 answers

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

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…
Gray
  • 115,027
  • 24
  • 293
  • 354
71
votes
7 answers

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

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…
cottonBallPaws
  • 21,220
  • 37
  • 123
  • 171
63
votes
6 answers

ExecutorService vs ThreadPoolExecutor using LinkedBlockingQueue

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…
user1813228
43
votes
4 answers

SingleThreadExecutor VS plain thread

Apart from the fact that the Executor interface has some advantages over plain threads (management, for example), is there any real internal difference (big performance difference, resource consumption...) between doing: ExecutorService executor =…
Magd Kudama
  • 3,229
  • 2
  • 21
  • 25
35
votes
4 answers

What's the advantage of a Java-5 ThreadPoolExecutor over a Java-7 ForkJoinPool?

Java 5 has introduced support for asynchronous task execution by a thread pool in the form of the Executor framework, whose heart is the thread pool implemented by java.util.concurrent.ThreadPoolExecutor. Java 7 has added an alternative thread pool…
28
votes
1 answer

Spring ThreadPoolTaskScheduler vs ThreadPoolTaskExecutor

It is mentioned in the Spring documentation that: ThreadPoolTaskScheduler actually implements Spring's TaskExecutor interface as well, so that a single instance can be used for asynchronous execution as soon as possible as well as scheduled, and…
chammu
  • 1,275
  • 1
  • 18
  • 26
27
votes
2 answers

How to create a scala.concurrent.ExecutionContext

How to create a scala.concurrent.ExecutionContext ? The documentation generally gives an overall summary and mentions the "default" implementation of scala.concurrent.ExecutionContext.global. Still, sometimes you have to create your personal E.C.,…
VasiliNovikov
  • 9,681
  • 4
  • 44
  • 62
23
votes
3 answers

Handling Exceptions for ThreadPoolExecutor

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…
jagamot
  • 5,348
  • 18
  • 59
  • 96
22
votes
3 answers

Correct way to communicate the result of a background thread to the Ui Thread in Android

This is one of the most confusing topics for me. So my question is, what is the correct way of communicate the result of background thread when this finish?. Imagine I want to update some TextView with some information I just downloaded.There is 3…
22
votes
2 answers

Is having a single threadpool better design than multiple threadpools

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.…
22
votes
5 answers

How to force terminate all workers in ThreadPoolExecutor immediately

I have a number of tasks inside a TheadPoolExecutor. I have a stop button on my interface that should terminate all the threads inside ThreadPoolExecutor immediately. I am looking for a way to do it. (without shutDown() or shutDownNow()). Thanks
dlock
  • 9,447
  • 9
  • 47
  • 67
1
2 3
72 73