Questions tagged [executor]

Use for questions related to nodes that are used to actually execute a distributed job/program/application.

Use for questions related to nodes that are used to actually execute a distributed job/program/application.

Synonym: Worker.

464 questions
190
votes
12 answers

Java executors: how to be notified, without blocking, when a task completes?

Say I have a queue full of tasks which I need to submit to an executor service. I want them processed one at a time. The simplest way I can think of is to: Take a task from the queue Submit it to the executor Call .get on the returned Future and…
Shahbaz
  • 10,395
  • 21
  • 54
  • 83
112
votes
16 answers

How to make ThreadPoolExecutor's submit() method block if it is saturated?

I want to create a ThreadPoolExecutor such that when it has reached its maximum size and the queue is full, the submit() method blocks when trying to add new tasks. Do I need to implement a custom RejectedExecutionHandler for that or is there an…
Fixpoint
  • 9,619
  • 17
  • 59
  • 78
88
votes
5 answers

Method call to Future.get() blocks. Is that really desirable?

Below is the snippet of the pseudo code. Does the below code not defeat the very notion of parallel asynchronous processing? The reason I ask this is because in the below code the main thread would submit a task to be executed in a different thread.…
76
votes
10 answers

ThreadPoolExecutor Block When its Queue Is Full?

I am trying to execute lots of tasks using a ThreadPoolExecutor. Below is a hypothetical example: def workQueue = new ArrayBlockingQueue(3, false) def threadPoolExecutor = new ThreadPoolExecutor(3, 3, 1L, TimeUnit.HOURS, workQueue) for(int…
ghempton
  • 7,777
  • 7
  • 48
  • 53
58
votes
7 answers

When should we use Java's Thread over Executor?

Executor seems like a clean abstraction. When would you want to use Thread directly rather than rely on the more robust executor?
ripper234
  • 222,824
  • 274
  • 634
  • 905
49
votes
5 answers

How to properly catch RuntimeExceptions from Executors?

Say that I have the following code: ExecutorService executor = Executors.newSingleThreadExecutor(); executor.execute(myRunnable); Now, if myRunnable throws a RuntimeExcpetion, how can I catch it? One way would be to supply my own ThreadFactory…
Joonas Pulakka
  • 36,252
  • 29
  • 106
  • 169
44
votes
3 answers

Detailed difference between Java8 ForkJoinPool and Executors.newWorkStealingPool?

What is the low-level difference among using: ForkJoinPool = new ForkJoinPool(X); and ExecutorService ex = Executors.newWorkStealingPool(X); Where X is the desired level of parallelism, i.e. threads running. According to the docs, I found them…
44
votes
8 answers

Java Executors: how can I set task priority?

Is there a possibility to set priority to tasks which are executed by Executors? I've found some statements in JCIP about it's possible but I cannot find any example and I cannot find anything related in docs. From JCIP: An execution policy…
Roman
  • 64,384
  • 92
  • 238
  • 332
43
votes
2 answers

Elegantly implementing queue length indicators to ExecutorServices

Why, oh why doesn't java.util.concurrent provide a queue length indicators for its ExecutorServices? Recently I found myself doing something like this: ExecutorService queue = Executors.newSingleThreadExecutor(); AtomicInteger queueLength = new…
Joonas Pulakka
  • 36,252
  • 29
  • 106
  • 169
37
votes
4 answers

Java Executor Best Practices for Tasks that Should Run Forever

I'm working on a Java project where I need to have multiple tasks running asynchronously. I'm led to believe Executor is the best way for me to do this, so I'm familiarizing myself with it. (Yay getting paid to learn!) However, it's not clear to…
BlairHippo
  • 9,502
  • 10
  • 54
  • 78
35
votes
4 answers

Unhandled exceptions with Java scheduled executors

I have the following issue and I would like to know what exactly happens. I am using Java's ScheduledExecutorService to run a task every five minutes. It works very well. Executors completely changed the way I do thread programming in Java. Now, I…
Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
33
votes
2 answers

asyncio: Is it possible to cancel a future been run by an Executor?

I would like to start a blocking function in an Executor using the asyncio call loop.run_in_executor and then cancel it later, but that doesn't seem to be working for me. Here is the code: import asyncio import time from concurrent.futures import…
Brendan Maguire
  • 4,188
  • 4
  • 24
  • 28
31
votes
10 answers

Removing all queued tasks of an ThreadPoolExecutor

i have this rather simple question about the ThreadPoolExecutor. I have the following situation: I have to consume objects from a queue, create the appropiate worker tasks for them and submit them to the ThreadPoolExecutor. This is quite simple. But…
Malax
  • 9,436
  • 9
  • 48
  • 64
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
1 answer

Any good Spring threading with a TaskExecutor examples?

I'm trying to get a handle on how to implement threading in a Java application that uses Spring for transaction management. I've found the TaskExecutor section in the Spring documentation, and ThreadPoolTaskExecutor looks like it would fit my…
James McMahon
  • 48,506
  • 64
  • 207
  • 283
1
2 3
30 31