executorservice

java Fork/Join pool, ExecutorService and CountDownLatch

孤街醉人 提交于 2019-11-27 01:43:20
问题 We have three different multi threading techniques in java - Fork/Join pool, Executor Service & CountDownLatch Fork/Join pool (http://www.javacodegeeks.com/2011/02/java-forkjoin-parallel-programming.html) The Fork/Join framework is designed to make divide-and-conquer algorithms easy to parallelize. That type of algorithms is perfect for problems that can be divided into two or more sub-problems of the same type. They use recursion to break down the problem to simple tasks until these become

How to pause/resume all threads in an ExecutorService in Java?

半城伤御伤魂 提交于 2019-11-27 01:11:48
问题 I submitted bunch of jobs to an executorservice in Java and I somehow want to temporarily pause all these jobs. What's the best way to do this? How can I resume? Or am I doing this completely wrong? Should I follow some other pattern for what I want to achieve (i.e. ability to pause/resume execution services)? 回答1: To answer my own question, I found an example of a PausableThreadPoolExecutor in the javadocs of ThreadPoolExecutor itself. Here is my version using Guava's Monitors: import com

producer/consumer work queues

痴心易碎 提交于 2019-11-27 00:58:58
问题 I'm wrestling with the best way to implement my processing pipeline. My producers feed work to a BlockingQueue. On the consumer side, I poll the queue, wrap what I get in a Runnable task, and submit it to an ExecutorService. while (!isStopping()) { String work = workQueue.poll(1000L, TimeUnit.MILLISECONDS); if (work == null) { break; } executorService.execute(new Worker(work)); // needs to block if no threads! } This is not ideal; the ExecutorService has its own queue, of course, so what's

ExecutorService's surprising performance break-even point — rules of thumb?

时间秒杀一切 提交于 2019-11-27 00:45:57
问题 I'm trying to figure out how to correctly use Java's Executors. I realize submitting tasks to an ExecutorService has its own overhead. However, I'm surprised to see it is as high as it is. My program needs to process huge amount of data (stock market data) with as low latency as possible. Most of the calculations are fairly simple arithmetic operations. I tried to test something very simple: " Math.random() * Math.random() " The simplest test runs this computation in a simple loop. The second

How to wait for a thread that spawns it's own thread?

只愿长相守 提交于 2019-11-26 23:35:16
问题 I'm trying to test a method that does it's work in a separate thread, simplified it's like this: public void methodToTest() { Thread thread = new Thread() { @Override public void run() { Clazz.i = 2; } }; thread.start(); } In my unit test I want to test that Clazz.i == 2, but I can't do this because I think that the assert is run before the thread changes the value. I thought of using another thread to test it and then use join to wait but it still doesn't work. SSCCE: @Test public void sscce

FixedThreadPool vs CachedThreadPool: the lesser of two evils

柔情痞子 提交于 2019-11-26 23:32:40
I have a program that spawns threads (~5-150) which perform a bunch of tasks. Originally, I used a FixedThreadPool because this similar question suggested they were better suited for longer lived tasks and with my very limited knowledge of multithreading, I considered the average life of the threads (several minutes) " long lived ". However, I recently added the capability to spawn additional threads and doing so takes me above the thread limit I set. In this case, would it be better to guess and increase the number threads I can allow or to switch to a CachedThreadPool so I have no wasted

ExecutorService.invokeAll does NOT support collection of runnable task

喜夏-厌秋 提交于 2019-11-26 23:20:16
问题 Wanted to run collection of Runnable task through invokeAll(..) method of ExecutorService. But that's not supported as of now ( supports collection of Callable task only ) Any specific reason for this? What's the alternative to do something similar. 回答1: Simply transform the runnables into callables: List<Callable<Void>> callables = new ArrayList<>(); for (Runnable r : runnables) { callables.add(toCallable(r)); } executor.invokeAll(callables); private Callable<Void> toCallable(final Runnable

How to catch exceptions in FutureTask

会有一股神秘感。 提交于 2019-11-26 23:12:57
问题 After finding that FutureTask running in a Executors.newCachedThreadPool() on Java 1.6 (and from Eclipse) swallows exceptions in the Runnable.run() method, I've tried to come up with a way to catch these without adding throw/catch to all my Runnable implementations. The API suggests that overriding FutureTask.setException() should help in this: Causes this future to report an ExecutionException with the given throwable as its cause, unless this Future has already been set or has been

What are the advantages of using an ExecutorService?

↘锁芯ラ 提交于 2019-11-26 21:53:52
What is the advantage of using ExecutorService over running threads passing a Runnable into the Thread constructor? andersoj ExecutorService abstracts away many of the complexities associated with the lower-level abstractions like raw Thread . It provides mechanisms for safely starting, closing down, submitting, executing, and blocking on the successful or abrupt termination of tasks (expressed as Runnable or Callable ). From JCiP , Section 6.2, straight from the horse's mouth: Executor may be a simple interface, but it forms the basis for a flexible and powerful framework for asynchronous

Understanding Java ExecutorService

蓝咒 提交于 2019-11-26 21:43:08
问题 I am trying the learn how to use executorservice of Java, I was reading the following discussion Java thread simple queue In this there is a sample example ExecutorService service = Executors.newFixedThreadPool(10); // now submit our jobs service.submit(new Runnable() { public void run() { do_some_work(); } }); // you can submit any number of jobs and the 10 threads will work on them // in order ... // when no more to submit, call shutdown service.shutdown(); // now wait for the jobs to