executorservice

Is it possible to interrupt a specific thread of an ExecutorService?

自古美人都是妖i 提交于 2019-12-01 16:38:24
问题 If I have an ExecutorService to which I feed Runnable tasks, can I select one and interrupt it? I know I can cancel the Future returned (also mentioned Here: how-to-interrupt-executors-thread), but how can I raise an InterruptedException . Cancel doesn't seem to do it (event though it should by looking at the sources, maybe the OSX implementation differs). At least this snippet doesn't print 'it!' Maybe I'm misunderstaning something and it's not the custom runnable that gets the exception?

How can I make shutdown work properly with this custom ExecutorService?

冷暖自知 提交于 2019-12-01 12:58:23
问题 I'm my code I submit some tasks to an ExecutorService and then wait for them to complete using shutdown() and awaitTermination(). But if any one tasks takes longer than a certain period to complete I want it cancelled without affecting other tasks. I use code amended code from ExecutorService that interrupts tasks after a timeout as follows: package com.jthink.jaikoz.memory; import com.jthink.jaikoz.MainWindow; import java.util.List; import java.util.concurrent.*; public class

Porting code from using timers to scheduledexecutorservice

会有一股神秘感。 提交于 2019-12-01 10:11:23
I am trying to port code from using java timers to using scheduledexecutorservice I have the following use case class A { public boolean execute() { try { Timer t = new Timer(); t.schedule (new ATimerTask(), period, delay); } catch (Exception e) { return false; } } } class B { public boolean execute() { try { Timer t = new Timer(); t.schedule (new BTimerTask(), period, delay); } catch (Exception e) { return false; } } } Should I just replace Timer instances in class A and class B with ScheduledExecutorService and make the ATimerTask and BTimerTask class to a Runnable class , for e.g class B {

Porting code from using timers to scheduledexecutorservice

隐身守侯 提交于 2019-12-01 09:22:49
问题 I am trying to port code from using java timers to using scheduledexecutorservice I have the following use case class A { public boolean execute() { try { Timer t = new Timer(); t.schedule (new ATimerTask(), period, delay); } catch (Exception e) { return false; } } } class B { public boolean execute() { try { Timer t = new Timer(); t.schedule (new BTimerTask(), period, delay); } catch (Exception e) { return false; } } } Should I just replace Timer instances in class A and class B with

ExecutorService slow multi thread performance

一笑奈何 提交于 2019-12-01 05:47:06
I am trying to execute a simple calculation (it calls Math.random() 10000000 times). Surprisingly running it in simple method performs much faster than using ExecutorService. I have read another thread at ExecutorService's surprising performance break-even point --- rules of thumb? and tried to follow the answer by executing the Callable using batches, but the performance is still bad How do I improve the performance based on my current code? import java.util.*; import java.util.concurrent.*; public class MainTest { public static void main(String[]args) throws Exception { new MainTest().start(

Abort countDownLatch.await() after time out

雨燕双飞 提交于 2019-12-01 03:47:28
I am using an ExecutorService to implement a 3-thread pool, and CountDownLatch to monitor the completion of all threads, for further processing. ExecutorService threadExecutor = Executors.newFixedThreadPool(3); CountDownLatch countDownLatch = new CountDownLatch(3); AuthorisationHistoryTask task1 = new AuthorisationHistoryTask(commonDataThread, countDownLatch ); PreAuthHistoryTask task2 = new PreAuthHistoryTask(userID,sessionID, commonDataThread, countDownLatch ); SettlementHistTask task4 = new SettlementHistTask(commonDataThread,countDownLatch); Future<Map<String, Object>> futureAuthHistory =

ExecutorService slow multi thread performance

我怕爱的太早我们不能终老 提交于 2019-12-01 02:25:36
问题 I am trying to execute a simple calculation (it calls Math.random() 10000000 times). Surprisingly running it in simple method performs much faster than using ExecutorService. I have read another thread at ExecutorService's surprising performance break-even point --- rules of thumb? and tried to follow the answer by executing the Callable using batches, but the performance is still bad How do I improve the performance based on my current code? import java.util.*; import java.util.concurrent.*;

ExecutorService's shutdown() doesn't wait until all threads will be finished

大城市里の小女人 提交于 2019-11-30 22:16:55
I have a code where 4 threads run at the same time. I want to wait until all these 4 threads will be finished. And only after that to continue the app flow. I tried two approaches: Thread#join() , this approach works as expected. The code, which comes after join() is executed only after all threads are finished. ExecutorService#shutdown() , this technique allows executing code, which comes after shutdown() even if not all threads are finished. Code sample: ExecutorService service = Executors.newFixedThreadPool(cpuCoresNum); for (int i = 0; i < cpuCoresNum; i++) { service.submit(() -> { try {

Using ExecutorService with a tree of tasks to perform

╄→尐↘猪︶ㄣ 提交于 2019-11-30 18:51:06
问题 We had a bit of a problem. :) We want to ensure that only N threads are doing background tasks at any time. To do this, we used a fixed thread pool executor. It seemed to be working fine. Then we found an issue. Suppose you have a class which uses the executor to do some parallel work and then it calls some other class while in the executor thread which also does some parallel work, intending to wait on it. Here's what happens: Main thread calls the first level method. This method thinks it

ExecutorService's shutdown() doesn't wait until all threads will be finished

北城以北 提交于 2019-11-30 17:26:49
问题 I have a code where 4 threads run at the same time. I want to wait until all these 4 threads will be finished. And only after that to continue the app flow. I tried two approaches: Thread#join(), this approach works as expected. The code, which comes after join() is executed only after all threads are finished. ExecutorService#shutdown(), this technique allows executing code, which comes after shutdown() even if not all threads are finished. Code sample: ExecutorService service = Executors