executor

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 block until a result is available Take another task from the queue... However, I am try...

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 needs; ThreadPoolTaskExecutor This implementation can only be used in a Java 5 environm...

newCachedThreadPool() V/s newFixedThreadPool

newCachedThreadPool() V/s newFixedThreadPool(...) when to use and which is better in terms of resource utilisation? ...

ThreadPool is to Executor like Polling is to ???

Java's Executor is (as far as I understand it) an abstraction over the ThreadPool concept - something that can accept and carry out (execute) tasks. I'm looking for a similar exception for the Polling concept. I need to continuously poll (dequeue) items out of a specific Queue (which does not implement BlockingQueue), execute them and s...

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? ...

Executor and Daemon in Java

Hi, I have a MyThread object which I instantiate when my app is loaded through the server, I mark it as a Daemon thread and then call start() on it. The thread is meant to sit and wait for information from a queue as long as the application is active. My problem/question is this: Currently MyThread is extending Thread because I mark it a...

Java Scheduled Executor: Does it guarantee to not run in paralell if task hast not yet finished

Hello, does anyone know if the following java Method in the java.util.concurrent Package ScheduledExecutorService.html#scheduleAtFixedRate() absolutely guarantees, that the Runnable scheduled will never run in parallel in case the runnable from the "last" run did not yet finish: For example (Pseudocode) 1.00 o'clock: scheduleAtFixedR...

Unhandled exceptions with Java scheduled executors

Hi, 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 browsed Java Doc for information about what would be the behavior in case the schedul...

Removing all queued tasks of an ThreadPoolExecutor

Hi StackOverflow, 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 within a shutdown scenario many workers may be queued to execution. Si...

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 implementation to newSingleThreadExecutor() and set custom uncaughtExceptionHandlers for the...

Wait for all threads in an Executor to finish?

I'm implementing a parellel quicksort as programming practice, and after I finished, I read the Java tutorial page on Executors, which sound like they could make my code even faster. Unfortunately, I was relying on join()'s to make sure that the program doesn't continue until everything is sorted. Right now I'm using: public static void...

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 sumbit() method blocks when trying to add new tasks. Do I need to implement a custom RejectedExecutionHandler for that or is there an existing way to do this using standard java library? ...

Java Executor Best Practices

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 me what the best way is to accomplish what I'm trying to do. For the sake of argument, le...

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 AtomicInteger(); ... public void addTaskToQueue(Runnable runnable) { if (queueLength.ge...

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 specifies the "what, where, when, and how" of task execution, including: ... ...

ThreadPoolExecutor policy

I'm trying to use a ThreadPoolExecutor to schedule tasks, but running into some problems with its policies. Here's its stated behavior: If fewer than corePoolSize threads are running, the Executor always prefers adding a new thread rather than queuing. If corePoolSize or more threads are running, the Executor always prefers queuing a r...

ThreadPoolExecutor Block When Queue Is Full?

I am trying to execute lots of tasks using a ThreadPoolExecutor. Below is a hypothetical example: def workQueue = new ArrayBlockingQueue<Runnable>(3, false) def threadPoolExecutor = new ThreadPoolExecutor(3, 3, 1L, TimeUnit.HOURS, workQueue) for(int i = 0; i < 100000; i++) threadPoolExecutor.execute(runnable) The problem is that I...

Java - what's so great about Executors?

In a life without Java Executors, new threads would have to be created for each Runnable tasks. Making new threads requires thread overhead (creation and teardown) that adds complexity and wasted time to a non-Executor program. Referring to code: no Java Executor - new Thread (aRunnableObject).start (); with Java Executor - Executo...

How to implement PriorityBlockingQueue with ThreadPoolExecutor and custom tasks

I've searched a lot but could not find a solutuion to my problem. I have my own class, BaseTask, that uses a ThreadPoolExecutor to handle tasks. If I don't want prioritization (i.e. using a LinkedBlockingQueue) this works just fine, but when I try to use a PriorityBlockingQueue I get ClassCastException because the ThreadPoolExecutor w...

Calling Looper more than once causes "sending message to a Handler on a dead thread"

I am using an Executor [fixed thread pool] with my own ThreadFactory that adds a Looper: Handler HANDLER = new Handler(); Executor THREADS = Executors.newFixedThreadPool(THREAD_POOL_SIZE, new ThreadFactory() { @Override public Thread newThread(Runnable runnable) { return new MyThread(new Runnable() { @Override...