executorservice

how to get Future return when cause timeout Exception in java

一世执手 提交于 2021-02-20 00:47:56
问题 I'm wondering can I get the result when it cause timeout Exception when use Future and ExecutorService, for example, below is my code package com.example; import java.util.UUID; import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import org.apache.commons.lang3.concurrent

how to get Future return when cause timeout Exception in java

房东的猫 提交于 2021-02-20 00:42:09
问题 I'm wondering can I get the result when it cause timeout Exception when use Future and ExecutorService, for example, below is my code package com.example; import java.util.UUID; import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import org.apache.commons.lang3.concurrent

Using a ScheduledExecutorService to run a task on a periodic basis in Java

送分小仙女□ 提交于 2021-02-11 15:00:49
问题 I am working on a program that will read data from a data source, and publish that data as it is read. I have a reader and a writer, the reader spawns several threads to read all the data it needs to read, puts the data into a queue, and the writer reads the data from the queue an publishes it. I have a controller for my readers and a controller for my writers. The controllers implement the Callable interface, but could implement the Runnable interface as my call return is Void . I want to

Using a ScheduledExecutorService to run a task on a periodic basis in Java

我的未来我决定 提交于 2021-02-11 14:59:42
问题 I am working on a program that will read data from a data source, and publish that data as it is read. I have a reader and a writer, the reader spawns several threads to read all the data it needs to read, puts the data into a queue, and the writer reads the data from the queue an publishes it. I have a controller for my readers and a controller for my writers. The controllers implement the Callable interface, but could implement the Runnable interface as my call return is Void . I want to

How do I know when all threads in a ExecutorService are finished?

人盡茶涼 提交于 2021-02-10 14:24:48
问题 I know that shutdown() and awaitTermination() exist. The problem is that the runnables in the pool need to be able to add an unknown number (can't use a countdownlatch) of other runnables to it and if I call shutdown() those tasks will be rejected. How can I know when they're done? 回答1: Instead of submitting Runnable tasks to an Executor , you should rather use ForkJoinTask/ForkJoinPool instead. A ForkJoinTask runs inside a ForkJoinPool and can spawn an arbitrary number of (sub)tasks and wait

Same Runnable class being passed twice to the ExecutorService running two/multiple threads at the same time

≡放荡痞女 提交于 2021-02-08 06:52:05
问题 I have multiple runnable classes and I want to run them with executor service through a centralized class (call it Launcher class which contains list of all runnables). I wrote a Launcher class which takes all the beans to instantiate using applicationContext.getBean() . Each runnable class also defines a pool size for it (number of threads to spawn for this runnable). public class DaemonsLauncher { @Autowired private ApplicationContext appContext; private List<Daemon> daemons = new ArrayList

Same Runnable class being passed twice to the ExecutorService running two/multiple threads at the same time

孤者浪人 提交于 2021-02-08 06:50:51
问题 I have multiple runnable classes and I want to run them with executor service through a centralized class (call it Launcher class which contains list of all runnables). I wrote a Launcher class which takes all the beans to instantiate using applicationContext.getBean() . Each runnable class also defines a pool size for it (number of threads to spawn for this runnable). public class DaemonsLauncher { @Autowired private ApplicationContext appContext; private List<Daemon> daemons = new ArrayList

Same Runnable class being passed twice to the ExecutorService running two/multiple threads at the same time

 ̄綄美尐妖づ 提交于 2021-02-08 06:50:48
问题 I have multiple runnable classes and I want to run them with executor service through a centralized class (call it Launcher class which contains list of all runnables). I wrote a Launcher class which takes all the beans to instantiate using applicationContext.getBean() . Each runnable class also defines a pool size for it (number of threads to spawn for this runnable). public class DaemonsLauncher { @Autowired private ApplicationContext appContext; private List<Daemon> daemons = new ArrayList

How to stop Callable tasks in Executor service if exception occur

天大地大妈咪最大 提交于 2021-01-29 08:51:10
问题 I'm trying to implement a sample application to test Callable and ExecutorService interfaces. In my app I have: @Bean("fixedThreadPool") public ExecutorService fixedThreadPool() { return Executors.newFixedThreadPool(5); } Then: public void removeUserIds(Set<String> userIds) { UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("http://localhost:8080/remove"); final List<Callable<String>> callables = new ArrayList<>(); userIds.forEach(userId -> { final Callable<String> task = ()

Java associate object with each thread in Executor

房东的猫 提交于 2021-01-29 02:13:41
问题 I have a bunch of runnables that I want to execute via a thread pool. However, each runnable also writes some result to some file. So right now, the runnable's interface is simple: class MyRunnable implements Runnable { ... MyRunnable(BufferedWriter writer, Task t) { this.writer = writer; this.task = t; } public void run() { ... this.writer.write(SOME_DATA); } } However, what I want is to associate one BufferedWriter (in other words, one output file) with each of the thread in the Executor