threadpoolexecutor

Threadpool with persistent worker instances

柔情痞子 提交于 2019-12-25 08:53:10
问题 I'm trying to queue up tasks in a thread pool to be executed as soon as a worker becomes free, i have found various examples of this but in all cases the examples have been setup to use a new Worker instance for each job, i want persistent workers. I'm trying to make a ftp backup tool, i have it working but because of the limitations of a single connection it is slow. What i ideally want to do is have a single connection for scanning directories and building up a file list then four workers

Using ThreadPoolExecutor and DiscardPolicy

心已入冬 提交于 2019-12-24 13:42:53
问题 I need to make a client queue with ThreadPoolExecutor and an ability to drop clients if it exceeds some number (5 for example). It is kinda DDOS protection. When client #6 is requesting my server - it got dropped, etc. I got my server and client code, but I don't know how to realize ThreadPoolExecutor and DiscardPolicy. Ideas or examples? Simple server: import java.io.IOException; import java.io.InputStream; import java.io.ObjectInputStream; import java.io.OutputStream; import java.net

ThreadPool / Executor on both Runnable and Callable - that can exits automatically java8

拈花ヽ惹草 提交于 2019-12-24 02:09:34
问题 I am trying to use a ThreadPoolExecutor/ExecutorService in my application - it is a static global object. I use: Executors.newScheduledThreadPool(corePoolSize) - but I am having problems with shutting down the executorService. If I don't call shutdown() + awaitTermination() - then my application won't finish -even if all threads are completed. My application has threads being created by other threads - so I can't put a shutdown() anywhere in the code without blocking further threads to be run

Force re-use of thread by CompletableFuture

允我心安 提交于 2019-12-24 00:49:47
问题 I am making critical use of: CompletableFuture .delayedExecutor(1, TimeUnit.MILLISECONDS).execute(() -> {}); From what I have read online, it's common for this to use a new thread for every call. I am wondering if there is a way to re-use a thread instead of creating new threads? Update : I wasn't clear - I want to use CompletableFuture , but I want CompletableFuture to reuse a certain thread, instead of managing its own threads. I see this question: CompletableFuture reuse thread from pool

Looking for solution based on ThreadPoolExecutor to ensure sequential execution of tasks

二次信任 提交于 2019-12-23 20:00:31
问题 I have a thread pool of m threads. Let's say m were 10 and fix. Then there are n queues with the possibility of n becoming large (like 100'000 or more). Every queue holds tasks to be executed by those m threads. Now, very important, every queue must be worked off sequentially task by task. This is a requirement to make sure that tasks are executed in the order they were added to the queue. Otherwise the data could become inconsistent (same as, say, with JMS queues). So the question is now how

implementing PriorityQueue on ThreadPoolExecutor

二次信任 提交于 2019-12-23 16:21:25
问题 Been struggling with this for over 2 days now. implemented the answer I saw on here Specify task order execution in Java public class PriorityExecutor extends ThreadPoolExecutor { public PriorityExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) { super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue); } //Utitlity method to create thread pool easily public static ExecutorService newFixedThreadPool(int nThreads)

ThreadPoolExecutor : : TaskRejectedException from Executor

别说谁变了你拦得住时间么 提交于 2019-12-22 08:23:19
问题 My application is reading messages through Jms MessageListener class and at some point of time it is throwing TaskRejectedException . I know most of you will say that the number of threads is exceeded by maxPoolSize and queue is also full. But I observed something. The number of messages sent to the queue from which the MessageListener class is fetching messages is 10353 and my spring property for threadPoolExecutor is below : <bean id="ticketReaderThreadPool" class="org.springframework

Do we need to shutdown ExecutorService fixedThreadPool

混江龙づ霸主 提交于 2019-12-22 08:14:11
问题 I have created threadpool using ExecutorService , in my application to call vendor websrvice, using below code. ExecutorService executor = Executors.newFixedThreadPool(getThreadPoolSize()); for (int i = 0; i < list.size(); i++) { Request ecpReq = list.get(i); thRespLst.add(executor.submit(new Task(ecpReq))); } Wanted to know do we need to take care of shutting down threadpool or something, basically I don't want hanging threads in production environment. 回答1: newFixedThreadPool public static

ThreadPoolExecutor vs ForkJoinPool: stealing subtasks

北城以北 提交于 2019-12-21 12:05:42
问题 From java docs, A ForkJoinPool differs from other kinds of ExecutorService mainly by virtue of employing work-stealing: all threads in the pool attempt to find and execute subtasks created by other active tasks (eventually blocking waiting for work if none exist). This enables efficient processing when most tasks spawn other subtasks (as do most ForkJoinTasks). When setting asyncMode to true in constructors, ForkJoinPools may also be appropriate for use with event-style tasks that are never

How to chose an Executor for CompletableFuture::supplyAsync

狂风中的少年 提交于 2019-12-20 12:04:05
问题 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 ? 回答1: You should use public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor) method. As executor you