executorservice

producer/consumer work queues

落爺英雄遲暮 提交于 2019-11-28 05:34:01
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 really happening is that I'm always fully draining my work queue and filling the task queue, which slowly

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

谁说胖子不能爱 提交于 2019-11-28 05:02:39
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 test does the same computation inside a anonymous Runnable (this is supposed to measure the cost of

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

試著忘記壹切 提交于 2019-11-28 01:51:45
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() throws InterruptedException { Thread thread = new Thread() { @Override public void run() {

Waiting for all the threads to finish before shutting down the Executors

北城余情 提交于 2019-11-28 01:43:12
问题 Here is my code snippet. ExecutorService executor = Executors.newFixedThreadPool(ThreadPoolSize); while(conditionTrue) { ClassImplementingRunnable c = new ClassImplementingRunnable(); executor.submit(c); } Now after this is do executor.shutdown(); What i want to achieve here is that i want to wait for all the threads in the threadpool to have finished the execution and then i want to shutdown the executor. But i guess this is not what is happening here. The main thread seems to be executing

ExecutorService.invokeAll does NOT support collection of runnable task

浪子不回头ぞ 提交于 2019-11-27 22:56:36
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. 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 runnable) { return new Callable<Void>() { @Override public Void call() { runnable.run(); return null; } };

How to catch exceptions in FutureTask

时光总嘲笑我的痴心妄想 提交于 2019-11-27 22:46:07
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 cancelled. This method is invoked internally by the run method upon failure of the computation. However this

How many AsyncTasks i can run in an single process application

风格不统一 提交于 2019-11-27 22:12:57
问题 I am using asyncTasks, to load list elements with images (Just followed android's tutorial of efficiently loading bitmaps) In DDMS, i can see upto 5 AsyncTasks being running Now in addition i have added another AsyncTask, which performs some decoding using MediaCodec class. Now in DDMS, i still see 5 AsyncTasks, and my image loading aynctask or decoding async task executes, not both of them. When decoding is running, if i scroll the list, elements' images are not updated Counterly when i

Stop a periodic task from within the task itself running in a ScheduledExecutorService

送分小仙女□ 提交于 2019-11-27 21:13:15
Is there a nice way to stop the repetition of task from within the task itself when running in a ScheduledExecutorService? Lets say, I have the following task: Future<?> f = scheduledExecutor.scheduleAtFixedRate(new Runnable() { int count = 0; public void run() { System.out.println(count++); if (count == 10) { // ??? cancel self } } }, 1, 1, TimeUnit.SECONDS); From outside, it is easy to cancel via f.cancel(), but how can I stop the repetition at the specified place? (Passing the Future through an AtomicReference is not safe, because there is a potential window when the scheduleAtFixedRate

shutdown and awaitTermination which first call have any difference?

断了今生、忘了曾经 提交于 2019-11-27 19:54:54
问题 What is the difference between ExecutorService eService = Executors.newFixedThreadPool(2); eService.execute(new TestThread6()); eService.execute(new TestThread6()); eService.execute(new TestThread6()); eService.awaitTermination(1, TimeUnit.NANOSECONDS); eService.shutdown(); and eService.shutdown(); eService.awaitTermination(1, TimeUnit.NANOSECONDS); I don't really understand shutdown() . This method does not wait for previously submitted tasks to complete execution. Does it mean shutdown()

Controlling Task execution order with ExecutorService

一笑奈何 提交于 2019-11-27 18:09:17
I have a process which delegates asynch tasks to a pool of threads. I need to ensure that certain tasks are executed in order. So for example Tasks arrive in order Tasks a1, b1, c1, d1 , e1, a2, a3, b2, f1 Tasks can be executed in any order except where there is a natural dependancy, so a1,a2,a3 must be processed in that order by either allocating to the same thread or blocking these until I know the previous a# task was completed. Currently it doesn't use the Java Concurrency package, but I'm considering changing to take avantage of the thread management. Does anyone have a similar solution