executorservice

What does 'Thread termination due to failure' refer to?

人走茶凉 提交于 2019-12-03 14:32:36
The javadoc for ExecutorService sometimes refers to the case when a Thread terminates 'due to failure'. However, it is not clear what kind of failure does this refer to. For instance, the single thread executor documentation says that if this single thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks I would have thought that this situation might happen in case of an Exception, or maybe a RuntimeException , but it does not seem to be the case. Running the following code seems to be giving the same thread

Does the future object returned by executorService.submit(Runnable) hold any reference to the runnable object?

橙三吉。 提交于 2019-12-03 11:38:28
Let's assume we have the following code: List<Future<?>> runningTasks; ExecutorService executor; ... void executeTask(Runnable task){ runningTasks.add(executor.submit(task)); } My questions are: Does runningTasks hold a reference to the task object? How long does it hold it for? Does it still hold it after the task is complete? In order to avoid memory leaks do I have to take care to remove the future that was added to the list? Until when the executor or the Future object holds a reference to it is an implementation detail . Therefore, if your tasks use a lot of memory such that you have to

Java ExecutorService callback on thread terminate

坚强是说给别人听的谎言 提交于 2019-12-03 11:02:29
I am using cached thread pool ExecutorService to run some asynchronous background tasks. I've provided my ThreadFactory that hands out threads to the ExecutorService (whenever it needs them). My understanding of the cached thread pool is that after the thread is sitting idle for 60 seconds it is termniated by the ExecutorService. I want to perform some state cleanup when my thread is about to be terminated. What is the best way to achieve this? The ExecutorService does not readily provide hooks into the thread's lifecycle. I don't want to shutdown my ExecutorService - useful for running tasks

How to name the threads of a thread pool in Java [duplicate]

杀马特。学长 韩版系。学妹 提交于 2019-12-03 10:34:15
问题 This question already has answers here : Naming threads and thread-pools of ExecutorService (17 answers) Closed 3 years ago . I have a Java application that uses the Executor framework and I have code that looks like this protected ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(5) My understanding is that internally the JVM would create a pool of 5 threads. Now when I check the execution in a profiler, I get something like thread-pool2,thread-pool3 and so

Elegantly implementing queue length indicators to ExecutorServices

谁说胖子不能爱 提交于 2019-12-03 10:32:56
问题 Why, oh why doesn't java.util.concurrent provide a queue length indicators for its ExecutorService s? Recently I found myself doing something like this: ExecutorService queue = Executors.newSingleThreadExecutor(); AtomicInteger queueLength = new AtomicInteger(); ... public void addTaskToQueue(Runnable runnable) { if (queueLength.get() < MAX_QUEUE_LENGTH) { queueLength.incrementAndGet(); // Increment queue when submitting task. queue.submit(new Runnable() { public void run() { runnable.run();

Java Executor Service Thread Pool [closed]

感情迁移 提交于 2019-12-03 09:12:47
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center . If I create a fixed size thread pool with 10 threads in java using Executor framework: private final ExecutorService pool; pool = Executors.newFixedThreadPool(10); and then try to submit more than 10 tasks (say for an example, 12 tasks); for (int i = 0 ; i < 12 ; i++) { pool.execute(new Handler(myRunnable)); } What will happen to the

Sleeping a thread inside an ExecutorService (Java/Clojure)

落花浮王杯 提交于 2019-12-03 08:22:44
I have a rather massive number of threads being created inside a clojure program: (import '(java.util.concurrent Executors)) (def *pool* (Executors/newCachedThreadPool)) (defn do-something [] ; work Thread/sleep 200 ; repeat) (dotimes [i 10000] (.submit *pool* do-something)) It's been a while between JVMs for me and I am basically wondering here if there is any argument against using sleep or yield inside the function that is being executed by the Executor? If I understand correctly, in this case, every one of my workers has it's own thread and therefore there should be no side effects. If the

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

巧了我就是萌 提交于 2019-12-03 08:08:42
问题 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 for some expert advice and maybe some new ideas. Existing questions here do not seem to focus primarily on performance, hence this new one. Thanks in advance!

How to use ExecutorService to poll until a result arrives

送分小仙女□ 提交于 2019-12-03 07:48:27
I have a scenario where I have to poll a remote server checking if a task has completed. Once it has, I make a different call to retrieve the result. I originally figured I should use a SingleThreadScheduledExecutor with scheduleWithFixedDelay for polling: ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); ScheduledFuture future = executor.scheduleWithFixedDelay(() -> poll(jobId), 0, 10, TimeUnit.SECONDS); public void poll(String jobId) { boolean jobDone = remoteServer.isJobDone(jobId); if (jobDone) { retrieveJobResult(jobId); } } But since I can only provide a

Can I use Callable threads without ExecutorService?

☆樱花仙子☆ 提交于 2019-12-03 07:15:32
问题 Can I use Callable threads without ExecutorService? We can use instances of Runnable and subclasses of Thread without ExecutorService and this code works normally. But this code works consistently: public class Application2 { public static class WordLengthCallable implements Callable { public static int count = 0; private final int numberOfThread = count++; public Integer call() throws InterruptedException { int sum = 0; for (int i = 0; i < 100000; i++) { sum += i; } System.out.println