executorservice

Java associate object with each thread in Executor

偶尔善良 提交于 2021-01-29 02:08:39
问题 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

Java associate object with each thread in Executor

≡放荡痞女 提交于 2021-01-29 02:07:54
问题 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

Execute and wait for multiple parallel and sequential Tasks by using a Arraylist of Tasks in JavaFX

我怕爱的太早我们不能终老 提交于 2021-01-28 09:36:30
问题 I'm looking for a suitable way to display the processing time of parallel running Tasks on a separate stage. I want to execute different tasks combined in an ArrayList - one after the other. For this case I'm using a ThreadPool. After each executed list, I want to wait until all tasks are completed. Only when the tasks have reached the status „succeeded“, I want to do something in the MainThread. After that I want to execute another list of tasks and visualize them on a separate stage as well

Java ExecutorService Read Tasks from Iterator

浪子不回头ぞ 提交于 2020-12-13 05:38:05
问题 All, I'm using a Java ExecutorService to perform tasks in parallel. Unfortunately, the list of tasks is now reaching the tens of millions. This means that submitting the tasks to the executor service ahead of time is infeasible due to memory constraints. I am able to generate an iterator which dynamically creates the tasks as they are needed, but I'm not sure how best to apply this to the ExecutorService. Should I create a task which pulls the next task from the iterator or is there some

How to unit test a code snippet running inside executor service, instead waiting on Thread.sleep(time)

感情迁移 提交于 2020-06-27 07:00:31
问题 How to unit test a code that is running in executor service? In my situation, public void test() { Runnable R = new Runnable() { @Override public void run() { executeTask1(); executeTask2(); } }; ExecutorService executorService = Executors.newSingleThreadExecutor(); executorService.submit(R); } When I am unit testing, I would like to make some validations that method executes. I am executing this in an executor service, as it makes some network operations. In my unit testing, I had to wait

ExecutorService JVM doesn't terminate [duplicate]

五迷三道 提交于 2020-06-26 04:45:12
问题 This question already has answers here : Java ServiceExecutor terminating condition (4 answers) Closed 5 years ago . I don't understand why I have to call executorService.shutdown() explicitly to terminate executorService. If I will not call shutdown() then the JVM will not terminate on its own. What is wrong with my program or what concept I am lacking? public class ExecutorServiceExample { public static class Task1 implements Runnable { @Override public void run() { try { Thread.sleep(1000)

What happens to the ThreadPoolExecutor when thread dies in Java

我只是一个虾纸丫 提交于 2020-06-23 02:47:12
问题 I have created a thread which in turn creates a ThreadPoolExecutor and submits some long running tasks to it. At some point, the original thread dies due to unhandled exception/error. What should happen to the executor (it's local to that dead thread, no external references to it)? Should it be GCed or not? EDIT: this question was formulated incorrectly from the beginning, but I will leave it as Gray provided some good details of how TPE work. 回答1: Threads are so called GC roots. This means

Spring - add a low priority multi threaded service (no impact to production performance)

那年仲夏 提交于 2020-06-10 01:50:08
问题 We have a Spring application, I want to add a service that will handle 10Ks IDs with multiple threads but will be as background process without impact production realtime . Service will update database and send external providers requests. I don't want service to impact/effect production performance /timing, I want to execute operation on each ID in a low priority I read previous post about setting priority in Executer, but I want low priority to all other threads that can be outside this

Handle back-pressure in FixedThreadPool

戏子无情 提交于 2020-06-01 05:12:26
问题 How to deal with back-pressure in Java using thread pool? How to reject new tasks so there are no more than N submitted tasks. N - is the maximum number of allowed tasks in submission queue, which include new, running, paused (not finished) tasks. Use case Users submit calculation tasks that run for some time . Sometimes, there are so many users submitting tasks at the same time. How to reject new tasks if there are already N tasks submitted. In other words, the total number of submitted (not