Questions tagged [executorservice]

ExecutorService is a Java object containing the managed pool of threads and capable of scheduling the submitted tasks for these threads. The scheduling strategies vary in multiple available implementations.

The number of threads in the service can be fixed or grow on demand or grow till some boundary is reached. The tasks that cannot be started immediately can be queued or the submitting thread may be suspended till the task can be accepted.

The task to submit must implement either Runnable or Callable interface (Callable allows some extended features).

When no longer needed, ExecutorService must be explicitly shutdown like a tiny OS.

2298 questions
456
votes
27 answers

How to wait for all threads to finish, using ExecutorService?

I need to execute some amount of tasks 4 at a time, something like this: ExecutorService taskExecutor = Executors.newFixedThreadPool(4); while(...) { taskExecutor.execute(new MyTask()); } //...wait for completion somehow How can I get notified…
serg
  • 109,619
  • 77
  • 317
  • 330
304
votes
6 answers

Java Timer vs ExecutorService?

I have code where I schedule a task using java.util.Timer. I was looking around and saw ExecutorService can do the same. So this question here, have you used Timer and ExecutorService to schedule tasks, what is the benefit of one using over…
kal
  • 28,545
  • 49
  • 129
  • 149
292
votes
20 answers

Naming threads and thread-pools of ExecutorService

Let's say I have an application that utilizes the Executor framework as such Executors.newSingleThreadExecutor().submit(new Runnable(){ @Override public void run(){ // do stuff } } When I run this application in the debugger, a…
mre
  • 43,520
  • 33
  • 120
  • 170
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
241
votes
16 answers

ExecutorService, how to wait for all tasks to finish

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 =…
george smiley
  • 2,721
  • 4
  • 21
  • 13
213
votes
7 answers

Choose between ExecutorService's submit and ExecutorService's execute

How should I choose between ExecutorService's submit or execute, if the returned value is not my concern? If I test both, I didn't see any differences among the two except the returned value. ExecutorService threadExecutor =…
Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875
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
150
votes
6 answers

How to get thread id from a thread pool?

I have a fixed thread pool that I submit tasks to (limited to 5 threads). How can I find out which one of those 5 threads executes my task (something like "thread #3 of 5 is doing this task")? ExecutorService taskExecutor =…
serg
  • 109,619
  • 77
  • 317
  • 330
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…
117
votes
11 answers

ExecutorService that interrupts tasks after a timeout

I'm looking for an ExecutorService implementation that can be provided with a timeout. Tasks that are submitted to the ExecutorService are interrupted if they take longer than the timeout to run. Implementing such a beast isn't such a difficult…
Edward Dale
  • 29,597
  • 13
  • 90
  • 129
112
votes
9 answers

What is the difference between ExecutorService.submit and ExecutorService.execute in this code in Java?

I am learning to use ExectorService to pool threads and send out tasks. I have a simple program below import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; class Processor…
brain storm
  • 30,124
  • 69
  • 225
  • 393
111
votes
3 answers

FixedThreadPool vs CachedThreadPool: the lesser of two evils

I have a program that spawns threads (~5-150) which perform a bunch of tasks. Originally, I used a FixedThreadPool because this similar question suggested they were better suited for longer lived tasks and with my very limited knowledge of…
111
votes
6 answers

Reason for calling shutdown() on ExecutorService

I was reading about it quite a bit in the past couple of hours, and I simply cannot see any reason (valid reason) to call shutdown() on the ExecutorService, unless we have a humongous application that stores, dozens and dozens of different executor…
Lucas
  • 3,521
  • 3
  • 27
  • 39
96
votes
9 answers

Turning an ExecutorService to daemon in Java

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…
Antiz
  • 1,424
  • 1
  • 13
  • 20
94
votes
8 answers

Java: ExecutorService that blocks on submission after a certain queue size

I am trying to code a solution in which a single thread produces I/O-intensive tasks that can be performed in parallel. Each task have significant in-memory data. So I want to be able limit the number of tasks that are pending at a moment. If I…
Tahir Akhtar
  • 11,385
  • 7
  • 42
  • 69
1
2 3
99 100