executorservice

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 overridden the "afterExecute" method which is supposed to provide any uncaught exceptions ...

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

Stop an infinite loop in an ExecutorService task

import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; class Task implements Callable<String> { public String call() t...

What is an elegant way for calling a custom close-method on each worker-thread in a Java threadpool?

say I'm using a ExecutorService ex = Executors.newFixedThreadPool(nrofthreads); spinning up some work and waiting when it's done. However I have same Threadlocal objects in the worker-threads which need to be closed when the batch is done. Therefore, I would like to be able to call a custom close-method on all worker-threads creat...

Does this require synchronization?

In the class below, I am using a singleThreadScheduledExecutor. My question is, do I need to synchronize around the access to dummyInt and dummyBoolean? import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class Playground { /** * @param arg...

JVM does not exit when TimeoutException occurs

I have code which needs to do something like this There is a list of classes each with some method (lets say execute()). I need to invoke that method on each class and there is a fixed timeOut for each invocation. Now, one of the class's execute method is badly written and results in a timeout due to which the jvm does not exit. I am ru...

No output from exception

Why does this code not print an exception stack trace? public class Playground { /** * @param args */ public static void main(String[] args) { startThread(); } private static void startThread() { ScheduledExecutorService timer = Executors .newSingleThreadScheduledExecutor(); ...

java.util.concurrent.Future.get() not returning

I have the following Java code: final Future future = exeService.submit( new Runnable() { public void run() { myObject.doSomething(); } } ); future.get(); where exeService is an instance of java.util.concurrent.ExecutorService The problem is that myObject.doSomething() never returns, and, hence...

Limiting one of each Runnable type in ExecutorService queue.

I have an Executors.newFixedThreadPool(1) that I send several different tasks to (all implementing Runnable), and they get queued up and run sequentially correct? What is the best way to only allow one of each task to be either running or queued up at one time? I want to ignore all tasks sent to the ExecutorService that are already in th...

ExecutionException and InterruptedException while using Future class's get() method

ExecutorService executor = Executors.newSingleThreadExecutor(); try { Task t = new Task(response,inputToPass,pTypes,unit.getInstance(),methodName,unit.getUnitKey()); Future<SCCallOutResponse> fut = executor.submit(t); response = fut.get(unit.getTimeOut(),TimeUnit.MILLISECONDS); ...

What is the fastest cyclic synchronization in Java (ExecutorService vs. CyclicBarrier vs. X)?

Which Java synchronization construct is likely to provide the best performance for a concurrent, iterative processing scenario with a fixed number of threads like the one outlined below? After experimenting on my own for a while (using ExecutorService and CyclicBarrier) and being somewhat surprised by the results, I would be grateful f...

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 task, but I'm wondering if anybody knows of an existing implementation. Here's what I came up...

Bigger threadpool or additional ExecutorService when new kind of thread will run?

Hi, I have a question that is related to possible overhead of ExecutorServices in Java. The present implementation has ExecutorService A with a capacity of 5 threads. It runs threads of type A. type A threads do some database reading and writing. Now, a thread of type B will run after some threads of type A has finished. The num...

Techniques for modeling a dynamic dataflow with Java concurrency API

Is there an elegant way to model a dynamic dataflow in Java? By dataflow, I mean there are various types of tasks, and these tasks can be "connected" arbitrarily, such that when a task finishes, successor tasks are executed in parallel using the finished tasks output as input, or when multiple tasks finish, their output is aggregated in ...

500 Worker Threads, what kind of thread pool?

I am wondering if this is the best way to do this. I have about 500 threads that run indefinitely, but Thread.sleep for a minute when done one cycle of processing. ExecutorService es = Executors.newFixedThreadPool(list.size()+1); for (int i = 0; i < list.size(); i++) { es.execute(coreAppVector.elementAt(i)); //coreAppVector ...

difference between thread.start() and executor.submit(thread)

hi, i am facing a problem regarding the thread. I am having a class which implements runnable, and i can use thread.start() method on that class. My question is i have one more class java.util.concurrent.ExecutorService in which i can call executor.submit(thread).. can anyone please tell me what is the difference between thread.start()...

what happens to running/blocked runnables when executorservice is shutdown()

I posted a question about a thread pattern today, and almost everyone suggested that I look into the ExecutorService. While I was looking into the ExecutorService, I think I am missing something. What happens if the service has a running or blocked threads, and someone calls ExecutorService.shutdown(). What happens to threads that are r...

stop thread that does not get interrupted

I have a thread that sits and reads objects off of an ObjectInputStream: public void run() { try { ois = new ObjectInputStream(clientSocket.getInputStream()); Object o; while ((o = ois.readObject()) != null) { //do something with object } } catch (Exception ex) { //Log excepti...

using ScheduledExecutorService to start and stop timer

From my readings, it seems that ScheduledExecutorService is the right way to start and stop timers in Java. I need to port some code that starts and stops a timer. This is not a periodic timer. This code, stops the timer before starting it. So, effectively every start is really a restart(). I am looking for the right way to do this usin...

Does ScheduledThreadPoolExecutor tie a thread to a task when it is scheduled?

Hi, I'm trying to understand if the ScheduledThreadPoolExecutor class "ties" a thread to a task when it is only scheduled or only during its run?(of course that when it's finished there is no relation).You can see in the link supplied that schedule(..) calls delayedExecute(...) which is ok but then delayedExecute calls prestartCoreThread...