executorservice

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

ぃ、小莉子 提交于 2019-12-03 06:59:45
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, and which one would perform better. Option 1 is to use the simple way: THREAD_POOL = Executors

Java - relationship between threads and CPUs

Deadly 提交于 2019-12-03 06:58:48
I am pretty new to multithreading, and I am working on a project where I am trying to utilize 4 CPUs in my Java program. I wanted to do something like int numProcessors = Runtime.getRuntime().availableProcessors(); ExecutorService e = Executors.newFixedThreadPool(numProcessors); Will this guarantee that I will have one thread working per CPU? At the time that I create the threads, the system will not be busy, however some time afterwards it will be extremely busy. I thought that the OS will pick the least busy CPU to create the threads, but how does it work if none of them are particularly

HashedWheelTimer vs ScheduledThreadPoolExecutor for higher performance

。_饼干妹妹 提交于 2019-12-03 06:58:33
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 task lock per each tick/task HashedWheelTimer O(1) adding new task O(m) per each timer tick (m ~ N/C where

How to decide whether to use newCachedThreadPool or newFixedThreadPool?

二次信任 提交于 2019-12-03 06:38:57
问题 I am working on a project in which I need to make sure each thread is working on a particular range. For example: NO_OF_THREADS: 2 NO_OF_TASKS: 10 If number of threads is 2 and number of tasks is 10 then each thread will be performing 10 tasks . So that means 2 thread will be doing 20 tasks . In actual scenario these numbers (number of tasks and number of threads) will be very high as both of them are configurable in my code. In the above example, first thread should be using id between 1 and

Detailed difference between Java8 ForkJoinPool and Executors.newWorkStealingPool?

♀尐吖头ヾ 提交于 2019-12-03 05:08:31
问题 What is the low-level difference among using: ForkJoinPool = new ForkJoinPool(X); and ExecutorService ex = Executors.neWorkStealingPool(X); Where X is the desired level of parallelism i.e threads running.. According to the docs I found them similar. Also tell me which one is more appropriate and safe under any normal uses. I have 130 million entries to write into a BufferedWriter and Sort them using Unix sort by 1st column. Also let me know how many threads to keep if possible. Note: My

Daemon threads scheduled on an ExecutorService; explain why this is bad form

落爺英雄遲暮 提交于 2019-12-03 04:56:05
问题 I'm comfortable with the idea of orderly shutdown on threads scheduled with an ExectuorService ; that is to say, calling shutdown or shutdownNow will cause threads created on the pool to exit gracefully. If they respond to interrupt you can be sure finally etc will be called and you'll get a clean, predictable exit (where you can cleanup any resources etc). However, if you've set your thread to be a daemon (via the executor's ThreadFactory ) as below. ExecutorService pool = Executors

Java support for three different concurrency models

一笑奈何 提交于 2019-12-03 04:52:26
问题 I am going through different concurrency model in multi-threading environment (http://tutorials.jenkov.com/java-concurrency/concurrency-models.html) The article highlights about three concurrency models . Parallel Workers The first concurrency model is what I call the parallel worker model. Incoming jobs are assigned to different workers . Assembly Line The workers are organized like workers at an assembly line in a factory. Each worker only performs a part of the full job. When that part is

Program does not terminate immediately when all ExecutorService tasks are done

ε祈祈猫儿з 提交于 2019-12-03 02:20:17
I put a bunch of runnable objects into an ExecutorService: // simplified content of main method ExecutorService threadPool = Executors.newCachedThreadPool(); for(int i = 0; i < workerCount; i++) { threadPool.execute(new Worker()); } I would expect my program/process to stop immediately after all workers are done. But according to my log, it takes another 20-30 seconds until that happens. The workers do not allocate any resources, in fact, they do nothing at the moment. Don't get me wrong, this is not a crucial problem for me, I'm just trying to understand what is happening and I'm wondering if

How to chose an Executor for CompletableFuture::supplyAsync

无人久伴 提交于 2019-12-03 02:09:07
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 ? You should use public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor) method. As executor you can use any from the Executors.new.. - it depends on your needs. It's better to use newFixedThreadPool()

How to name the threads of a thread pool in Java [duplicate]

◇◆丶佛笑我妖孽 提交于 2019-12-03 01:04:16
This question already has an answer here: Naming threads and thread-pools of ExecutorService 17 answers I have a Java application that uses the Executor framework and I have code that looks like this protected ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(5) My understanding is that internally the JVM would create a pool of 5 threads. Now when I check the execution in a profiler, I get something like thread-pool2,thread-pool3 and so on. Some of these thread pools are created by the server and some are created by me , I need a way to differentiate which