executorservice

ExecutorService vs Casual Thread Spawner

我与影子孤独终老i 提交于 2019-11-27 09:02:36
I have a basic question about how ExecutorService works in Java. It is quite hard to see the difference between simply creating Threads to perform some tasks in parallel and assigning each tasks to the ThreadPool . The ExecutorService also looks very simple and efficient to use, so I was wondering why we don't use it all the time. Is it just a matter of one way executing its job faster than the other ? Here's two very simple examples to show the difference between the two ways : Using executor service: Hello World (task) static class HelloTask implements Runnable { String msg; public HelloTask

Java Executors: how can I stop submitted tasks?

泪湿孤枕 提交于 2019-11-27 08:29:01
I have submitted a task using executors and I need it to stop after some time (e.g. 5 minutes). I have tried doing like this: for (Future<?> fut : e.invokeAll(tasks, 300, TimeUnit.SECONDS)) { try { fut.get(); } catch (CancellationException ex) { fut.cancel(true); tasks.clear(); } catch(ExecutionException ex){ ex.printStackTrace(); //FIXME: gestita con printstack } } But I always get an error: I have a shared Vector that needs to be modified by the tasks and then read by a thread, and even if I stop all the task, if the timeout occurs I get: Exception in thread "Thread-1" java.util

Java: ExecutorService that blocks on submission after a certain queue size

心已入冬 提交于 2019-11-27 06:01:39
I am trying to code a solution in which a single thread produces I/O-intensive tasks that can be performed in parallel. Each task have significant in-memory data. So I want to be able limit the number of tasks that are pending at a moment. If I create ThreadPoolExecutor like this: ThreadPoolExecutor executor = new ThreadPoolExecutor(numWorkerThreads, numWorkerThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(maxQueue)); Then the executor.submit(callable) throws RejectedExecutionException when the queue fills up and all the threads are already busy. What can I do to make

ScheduledExecutorService with variable delay

偶尔善良 提交于 2019-11-27 05:44:50
问题 Suppose I have a task that is pulling elements from a java.util.concurrent.BlockingQueue and processing them. public void scheduleTask(int delay, TimeUnit timeUnit) { scheduledExecutorService.scheduleWithFixedDelay(new Task(queue), 0, delay, timeUnit); } How can I schedule / reschedule the task if the frequency can be changed dynamically? The idea is to take a stream of data updates and propagate them in batch to a GUI The user should be able to vary the frequency of updates 回答1: I don't

Reason for calling shutdown() on ExecutorService

試著忘記壹切 提交于 2019-11-27 05:06:05
问题 I was reading about it quite a bit in past couple hours, and I simply cannot see any reason ( valid reason) to call shutdown() on the ExecutorService , unless we have a humongous application which stores, dozens and dozens of different executor services that are not used for a long time. The only thing (from what I gather) the shutdown does, is do what a normal Thread does once it's done. When the normal Thread will finish the run method of the Runnable(or Callable), it will be passed to

Controlling Task execution order with ExecutorService

假装没事ソ 提交于 2019-11-27 04:16:11
问题 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

How to use invokeAll() to let all thread pool do their task?

拥有回忆 提交于 2019-11-27 03:51:36
ExecutorService pool=Executors.newFixedThreadPool(7); List<Future<Hotel>> future=new ArrayList<Future<Hotel>>(); List<Callable<Hotel>> callList = new ArrayList<Callable<Hotel>>(); for(int i=0;i<=diff;i++){ String str="2013-"+(liDates.get(i).get(Calendar.MONTH)+1)+"-"+liDates.get(i).get(Calendar.DATE); callList.add(new HotelCheapestFare(str)); } future=pool.invokeAll(callList); for(int i=0;i<=future.size();i++){ System.out.println("name is:"+future.get(i).get().getName()); } Now I want pool to invokeAll all the task before getting to the for loop but when I run this program for loop gets

Java ExecutorService: awaitTermination of all recursively created tasks

老子叫甜甜 提交于 2019-11-27 03:50:24
I use an ExecutorService to execute a task. This task can recursively create other tasks which are submitted to the same ExecutorService and those child tasks can do that, too. I now have the problem that I want to wait until all the tasks are done (that is, all tasks are finished and they did not submit new ones) before I continue. I cannot call ExecutorService.shutdown() in the main thread because this prevents new tasks from being accepted by the ExecutorService . And Calling ExecutorService.awaitTermination() seems to do nothing if shutdown hasn't been called. So I am kinda stuck here. It

Java ExecutorService pause/resume a specific thread

爱⌒轻易说出口 提交于 2019-11-27 02:18:32
问题 Is there a way to use ExecutorService to pause/resume a specific thread? private static ExecutorService threadpool = Executors.newFixedThreadPool(5); Imagine that I want to stop the thread wich as the id=0 (assuming that to each one is assigned an incremental id until the size of the threadpool is reached). After a while, by pressing a button let's say, I want to resume that specific thread and leave all the other threads with their current status, which can be paused or resumed. I have found

Whether to use invokeAll or submit - java Executor service

孤街醉人 提交于 2019-11-27 01:53:20
问题 I have a scenario where I have to execute 5 thread asynchronously for the same callable. As far as I understand, there are two options: 1) using submit(Callable) ExecutorService executorService = Executors.newFixedThreadPool(5); List<Future<String>> futures = new ArrayList<>(); for(Callable callableItem: myCallableList){ futures.add(executorService.submit(callableItem)); } 2) using invokeAll(Collections of Callable) ExecutorService executorService = Executors.newFixedThreadPool(5); List