executorservice

Java executor with no ability to queue tasks

烂漫一生 提交于 2019-12-10 20:52:22
问题 I need an Java executor that rejects tasks if some other task is processing. I guess it's not possible to get manipulating the working queue size. Someone might wonder why I need an executor with such characteristic in the first place. I need an ability to easily change the policy and allow non-zero queue size. Any ideas? 回答1: Use a ThreadPoolExecutor with a SynchronousQueue (copied from this answer). It appears to work: import java.util.concurrent.RejectedExecutionHandler; import java.util

Java Process did not quit after Future task completion

半世苍凉 提交于 2019-12-10 18:09:23
问题 This is my code snippet using Future. import java.util.concurrent.*; import java.util.*; public class FutureDemo{ public FutureDemo(){ /* Future */ ExecutorService service = Executors.newFixedThreadPool(2); for ( int i=0; i<10; i++){ MyCallable myCallable = new MyCallable((long)i); Future<Long> futureResult = service.submit(myCallable); Long result = null; try{ result = futureResult.get(5000, TimeUnit.MILLISECONDS); }catch(TimeoutException e){ System.out.println("Time out after 5 seconds");

Java ExecutorService heap space problems

房东的猫 提交于 2019-12-10 17:46:47
问题 I hava a Java mulithreading question. I have the following worker class: public class ThreadWorker implements Runnable { //some code in here public void run(){ // invokes some recursion method in the ThreadWorker itself, // which will stop eventually { } To work with threads I'm using an ExecutorService : public static int THREAD_NUMBER = 4; public static ExecutorServide es = Executors.newFixedThreadPool(THREAD_NUMBER); Adding instances of ThreadWroker class happens here: public void

traversing a List<Future> object throws IndexOutOfBounds exception

一个人想着一个人 提交于 2019-12-10 15:41:57
问题 I have an ExecutorService which is used to invoke a Collection of Callable obejcts and returns a List of Future objects corresponding to Callable elements in the collection. However, somewhere while traversing the list, it throws the following exception : java.util.concurrent.ExecutionException: java.lang.IndexOutOfBoundsException: Index: 7, Size: 1 at java.util.concurrent.FutureTask.report(Unknown Source) at java.util.concurrent.FutureTask.get(Unknown Source) at com.soc.transformcsv

ScheduledThreadPoolExecutors and custom queue

痴心易碎 提交于 2019-12-10 15:20:32
问题 If I use a ThreadPoolExecutor I have a variety of constructors and I can pass/use my own queue for the pool's work queue. Now I see that a ScheduledThreadPoolExecutor is a subclass of ThreadPoolExecutor but the constructors are much less. Is there a way to use a ScheduledThreadPoolExecutor and still use my own work queue? 回答1: You can extend ScheduledThreadPoolExecutor class and use a different queue then the DelayedWorkQueue that is bound to the current ScheduledThreadPoolExecutor

How newCachedThreadPool reuses threads?

 ̄綄美尐妖づ 提交于 2019-12-10 13:53:54
问题 The javadoc says that the service returned by Executors.newCachedThreadPool reuses threads. How is this possible? A thread can only be started once by calling start . So how do they implement it? Threads of this service are running in an infinite loop and their Runnable -s are replaced on demand? 回答1: An Runnable can call another Runnable. Each thread runs only one main Runnable, but that Runnable takes Runnables from a shared BlockingQueue and calls these until it is shutdown. Simplified it

Knowing when all threads complete and dealing with exceptions

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 10:05:51
问题 I am using the Executor framework to kick off several threads using a threadpool i.e newFixedThreadPool. I use threadpool.submit(aThread) to submit jobs to be executed by the threadpool and this works fine however I need to determine when all the threads are complete so that I can continue with other processing. I looked at using Future.get() which blocks until the thread is complete the problem here being that it blocks until a result is available. I also looked at using continuously calling

Java: How to get finished threads to pickup tasks from running threads

一世执手 提交于 2019-12-09 22:51:01
问题 I am working on a multithreaded application with tasks that have varying run times. When one thread finishes, is there a way for it to take over some tasks from a still running thread? Here is an example. I kick off my program with 5 threads, and each have 50 tasks. When the quickest running thread finishes, another thread still has 40 tasks to complete. How can I get the finished thread to take 20 tasks from the other thread, so each continue working on 20 a piece, rather than waiting for

Stop a Runnable submitted to ExecutorService

放肆的年华 提交于 2019-12-09 16:22:23
问题 I've implemented subscription in my Java app. When new subscriber added, the application creates new task (class which implements Runnable to be run in the separate thread) and it is added to the ExecutorService like: public void Subscribe() { es_.execute(new Subscriber(this, queueName, handler)); } //... private ExecutorService es_; Application may register as many subscribers as you want. Now I want implement something like Unsubscribe so every subscriber has an ability to stop the message

ExecutorService that executes tasks sequentially but takes threads from a pool

99封情书 提交于 2019-12-09 15:08:34
问题 I am trying to build an implementation of the ExecutorService , let's call it SequentialPooledExecutor , with the following properties. All instances of SequentialPooledExecutor share the same thread pool Calls on the same instance of SequentialPooledExecutor are executed sequentially. In other words, the instance waits for the termination of the currently executing task before start processing the next task in its queue. I am currently in the process of implementing the