executorservice

Does the future object returned by executorService.submit(Runnable) hold any reference to the runnable object?

主宰稳场 提交于 2019-12-21 03:47:07
问题 Let's assume we have the following code: List<Future<?>> runningTasks; ExecutorService executor; ... void executeTask(Runnable task){ runningTasks.add(executor.submit(task)); } My questions are: Does runningTasks hold a reference to the task object? How long does it hold it for? Does it still hold it after the task is complete? In order to avoid memory leaks do I have to take care to remove the future that was added to the list? 回答1: Until when the executor or the Future object holds a

Optimal way of creating a fixed size thread pool in Java using the Executors service

谁都会走 提交于 2019-12-21 01:19:35
问题 I am using the Executors framework in Java to create thread pools for a multi-threaded application, and I have a question related to performance. I have an application which can work in realtime or non-realtime mode. In case it's realtime, I'm simply using the following: THREAD_POOL = Executors.newCachedThreadPool(); But in case it's not realtime, I want the ability to control the size of my thread pool. To do this, I'm thinking about 2 options, but I don't really understand the difference,

Sleeping a thread inside an ExecutorService (Java/Clojure)

亡梦爱人 提交于 2019-12-21 01:06:33
问题 I have a rather massive number of threads being created inside a clojure program: (import '(java.util.concurrent Executors)) (def *pool* (Executors/newCachedThreadPool)) (defn do-something [] ; work Thread/sleep 200 ; repeat) (dotimes [i 10000] (.submit *pool* do-something)) It's been a while between JVMs for me and I am basically wondering here if there is any argument against using sleep or yield inside the function that is being executed by the Executor? If I understand correctly, in this

HashedWheelTimer vs ScheduledThreadPoolExecutor for higher performance

浪子不回头ぞ 提交于 2019-12-21 00:11:52
问题 I'm figuring what a timer implementation to use if you need to schedule tons of (non blocking) tasks as fast as possible inside jvm on one machine. I've studied ScheduledThreadPoolExecutor and HashedWheelTimer sources (+wheel timer general docs) and here are basic differences (N - number of all outstanding scheduled tasks so far, C - wheel size): ScheduledThreadPoolExecutor O(log N) for adding new task O(1) per each timer tick (but tick per each task, so N overall) O(log N) cancelling the

How to chose an Executor for CompletableFuture::supplyAsync

狂风中的少年 提交于 2019-12-20 12:04:05
问题 CompletableFuture::supplyAsync(() -> IO bound queries) How do I chose an Executor for CompletableFuture::supplyAsync to avoid polluting the ForkJoinPool.commonPool() . There are many options in Executors ( newCachedThreadPool , newWorkStealingPool , newFixedThreadPool etc) And I read about new ForkJoinPool here How do I chose the right one for my use case ? 回答1: You should use public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor) method. As executor you

How to configure and tune Akka Dispatchers

£可爱£侵袭症+ 提交于 2019-12-20 10:23:29
问题 I'm looking over the documentation here: http://doc.akka.io/docs/akka/2.3.3/java/dispatchers.html We're using Akka in such a way where we have two separate dispatchers (default fork-join executors) for different actors. We're now running into some performance issues and we're looking into how we can tune the dispatcher configuration parameters and see how they affect the performance of the application. I've looked over the documentation but don't really understand the configuration parameters

Java ServiceExecutor terminating condition

怎甘沉沦 提交于 2019-12-20 03:42:14
问题 I'm new to java executor stuff. I'm using Java's ExecutorService to launch several threads to process data. Executor executor = Executors.newFixedThreadPool(poolSize); for(int i=0; i< 5;i++) executor.execute(new MyRunnable(i)); once the threads don't find data, they gracefully terminate. My question is what happens to the Executor when all the threads terminate, is it still running its master thread ? or it will terminate itself and whole application will finish gracefully? in case executor

What is happening underneath the Future.cancel(true)

我的梦境 提交于 2019-12-20 02:32:55
问题 Suppose I have a Runnable instance: class MyTask implements Runnable { public void run() { //some heavy calculation which takes time Thread.sleep(5000) //rest code ... } } Then, I use ExecutorService to submit the above task: ExecutorService service = Executors.newFixedThreadPool(3); Future<?> task = service.submit(new MyTask()); Now, I can cancel the task by task.cancel(true); . What I have understood is that the task.cancel(true) will interrupt the working thread in which this task is

BlockingQueue vs PipedOutputStream and PipedInputStream

不打扰是莪最后的温柔 提交于 2019-12-19 11:19:10
问题 I want to know Advantages to use BlockingQueue instead of ( PipedOutputStream and PipedInputStream ) import java.io.*; import java.util.concurrent.*; public class PipedStreamVsBlocking { public static void main(String... args) { BlockingQueue<Integer> blockingQueue = new LinkedBlockingDeque<>(2); ExecutorService executor = Executors.newFixedThreadPool(4); Runnable producerTask = () -> { try { while (true) { int value = ThreadLocalRandom.current().nextInt(0, 1000); blockingQueue.put(value);

How to stop immediately a task which is started using an ExecutorService?

纵然是瞬间 提交于 2019-12-19 10:08:15
问题 I have tried many different ways to immediately stop a task which is started using an ExecutorService, with no luck. Future<Void> future = executorService.submit(new Callable<Void>( public Void call () { ... do many other things here.. if(Thread.currentThread.isInterrupted()) { return null; } ... do many other things here.. if(Thread.currentThread.isInterrupted()) { return null; } } )); if(flag) { // may be true and directly cancel the task future.cancel(true); } Sometimes I need to cancel