executorservice

ExecutorService vs Casual Thread Spawner

房东的猫 提交于 2019-11-26 17:47:56
问题 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

How to stop all runnable thread in java executor class?

▼魔方 西西 提交于 2019-11-26 16:35:56
问题 final ExecutorService executor = Executors.newFixedThreadPool(1); final Future<?> future = executor.submit(myRunnable); executor.shutdown(); if(executor.awaitTermination(10, TimeUnit.SECONDS)) { System.out.println("task completed"); }else{ System.out.println("Executor is shutdown now"); } //MyRunnable method is defined as task which I want to execute in a different thread. Here is run method of executor class: public void run() { try { Thread.sleep(20 * 1000); } catch (InterruptedException e)

Shutdown ExecutorService gracefully in webapp?

与世无争的帅哥 提交于 2019-11-26 16:27:10
问题 In my webapp, I created a service that is using an ExecutorService with fixed size ThreadPool. I reuse the same ExecutorService during the whole application lifetime. private static ExecutorService pool = Executors.newFixedThreadPool(8); All is running in Tomcat which gives me the following error while shuting down: appears to have started a thread named [pool-1-thread-1] but has failed to stop it. This is very likely to create a memory leak. I do realize I need to shutdown the

Removing all queued tasks of an ThreadPoolExecutor

天大地大妈咪最大 提交于 2019-11-26 16:23:08
问题 i have this rather simple question about the ThreadPoolExecutor. I have the following situation: I have to consume objects from a queue, create the appropiate worker tasks for them and submit them to the ThreadPoolExecutor. This is quite simple. But within a shutdown scenario many workers may be queued to execution. Since one of those tasks might be running for an hour, and i want a relativly fast graceful shutdown of the application i want to discard all queued tasks from the

How to properly use Java Executor?

时光总嘲笑我的痴心妄想 提交于 2019-11-26 16:17:43
I've used Java Executors in my multi-threading apps, but I can't seem to figure out when is the best to use each of the following ways: 1. ExecutorService executor=Executors.newFixedThreadPool(50); executor.execute(new A_Runner(... some parameter ...)); executor.shutdown(); while (!executor.isTerminated()) { Thread.sleep(100); } 2. int Page_Count=200; ExecutorService executor=Executors.newFixedThreadPool(50); doneSignal=new CountDownLatch(Page_Count); for (int i=0;i<Page_Count;i++) executor.execute(new A_Runner(doneSignal, ... some parameter ...)); doneSignal.await(); executor.shutdown();

Executors.newCachedThreadPool() versus Executors.newFixedThreadPool()

风流意气都作罢 提交于 2019-11-26 15:38:12
newCachedThreadPool() versus newFixedThreadPool() When should I use one or the other? Which strategy is better in terms of resource utilization? bruno conde I think the docs explain the difference and usage of these two functions pretty well: newFixedThreadPool Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue. At any point, at most nThreads threads will be active processing tasks. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available. If any thread terminates due to a failure

Java Executors: how can I stop submitted tasks?

若如初见. 提交于 2019-11-26 14:08:45
问题 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

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

陌路散爱 提交于 2019-11-26 11:48:42
问题 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

ThreadPoolExecutor Block When Queue Is Full?

家住魔仙堡 提交于 2019-11-26 10:20:36
问题 I am trying to execute lots of tasks using a ThreadPoolExecutor. Below is a hypothetical example: def workQueue = new ArrayBlockingQueue<Runnable>(3, false) def threadPoolExecutor = new ThreadPoolExecutor(3, 3, 1L, TimeUnit.HOURS, workQueue) for(int i = 0; i < 100000; i++) threadPoolExecutor.execute(runnable) The problem is that I quickly get a java.util.concurrent.RejectedExecutionException since the number of tasks exceeds the size of the work queue. However, the desired behavior I am

Java&#39;s Fork/Join vs ExecutorService - when to use which?

时光总嘲笑我的痴心妄想 提交于 2019-11-26 08:58:53
问题 I just finished reading this post: What's the advantage of a Java-5 ThreadPoolExecutor over a Java-7 ForkJoinPool? and felt that the answer is not straight enough. Can you explain in simple language and examples, what are the trade-offs between Java 7\'s Fork-Join framework and the older solutions? I also read the Google\'s #1 hit on the topic Java Tip: When to use ForkJoinPool vs ExecutorService from javaworld.com but the article doesn\'t answer the title question when , it talks about api