executorservice

Elegantly implementing queue length indicators to ExecutorServices

泪湿孤枕 提交于 2019-12-03 01:04:15
Why, oh why doesn't java.util.concurrent provide a queue length indicators for its ExecutorService s? Recently I found myself doing something like this: ExecutorService queue = Executors.newSingleThreadExecutor(); AtomicInteger queueLength = new AtomicInteger(); ... public void addTaskToQueue(Runnable runnable) { if (queueLength.get() < MAX_QUEUE_LENGTH) { queueLength.incrementAndGet(); // Increment queue when submitting task. queue.submit(new Runnable() { public void run() { runnable.run(); queueLength.decrementAndGet(); // Decrement queue when task done. } }); } else { // Trigger error: too

What is the difference between ExecutorService.submit and ExecutorService.execute in this code in Java?

眉间皱痕 提交于 2019-12-03 01:00:46
问题 I am learning to use ExectorService to pool threads and send out tasks. I have a simple program below import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; class Processor implements Runnable { private int id; public Processor(int id) { this.id = id; } public void run() { System.out.println("Starting: " + id); try { Thread.sleep(5000); } catch (InterruptedException e) { System.out.println("sorry, being interupted, good bye!")

What is the fastest cyclic synchronization in Java (ExecutorService vs. CyclicBarrier vs. X)?

五迷三道 提交于 2019-12-02 21:59:28
Which Java synchronization construct is likely to provide the best performance for a concurrent, iterative processing scenario with a fixed number of threads like the one outlined below? After experimenting on my own for a while (using ExecutorService and CyclicBarrier) and being somewhat surprised by the results, I would be grateful for some expert advice and maybe some new ideas. Existing questions here do not seem to focus primarily on performance, hence this new one. Thanks in advance! The core of the app is a simple iterative data processing algorithm, parallelized to the spread the

How to configure and tune Akka Dispatchers

心已入冬 提交于 2019-12-02 21:03:15
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. For example, just for the simple default, fork-join-executor dispatcher: What are these and how

Can I use Callable threads without ExecutorService?

☆樱花仙子☆ 提交于 2019-12-02 20:48:23
Can I use Callable threads without ExecutorService? We can use instances of Runnable and subclasses of Thread without ExecutorService and this code works normally. But this code works consistently: public class Application2 { public static class WordLengthCallable implements Callable { public static int count = 0; private final int numberOfThread = count++; public Integer call() throws InterruptedException { int sum = 0; for (int i = 0; i < 100000; i++) { sum += i; } System.out.println(numberOfThread); return numberOfThread; } } public static void main(String[] args) throws

How to decide whether to use newCachedThreadPool or newFixedThreadPool?

随声附和 提交于 2019-12-02 20:31:01
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 10 and second thread should be using id between 11 and 20 and so on if any more threads. And after

Control ExecutorService to execute N tasks per second maximum

梦想的初衷 提交于 2019-12-02 19:47:43
How to control/limit the tasks that are submitted to a ExecutorService ? I have SMSTask that sends SMS messages and I need to control the executor so that it can only send at maximum N messages/second. Assuming you are creating one SMS message per task you can use a ScheduleExecutorService. final Queue<Task> tasks = new ConcurrentLinkedQueue<Task>(); int ratePerSecond = 10; final ExecutorService es = Executors.newCachedThreadPool(); ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor(); ses.scheduleAtFixedRate(new Runnable() { @Override public void run() { final Task task

Detailed difference between Java8 ForkJoinPool and Executors.newWorkStealingPool?

落爺英雄遲暮 提交于 2019-12-02 18:22:50
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 System has 8 core processors and 32 GB RAM. Work stealing is a technique used by modern thread-pools in

Issue when executing asynchronous tasks using ExecutorService

余生颓废 提交于 2019-12-02 18:07:52
问题 I had asked a question earlier regarding ExecutorService and Apache Velocity initialization. To give a quick recap -- I have a Java EE frontend which accepts user requests and then for each of these requests, uses ExecutorService(SingleThreadedExecutor set as a daemon) to kick off a lengthy workflow.This workflow is contained in a library and works well and as expected when run in a standalone mode through eclipse. When called from the website(servlet) I observed that the workflows were

Update JProgressBar from ExecutorService

时间秒杀一切 提交于 2019-12-02 15:43:30
问题 I am pinging gateways using Java ICMP ping function. To perform fast pinging I am using ExectorService which creates threads for pinging. After address is pinged (or not) I want to update Jprogressbar after pinging. I have this code which is working but it updates Jprogressbar before job (ping thread) is finished. I want to update jprogressbar after job is finished. private int NUM_THREADS = Runtime.getRuntime().availableProcessors(); ExecutorService exec = Executors.newFixedThreadPool(NUM