threadpoolexecutor

Does awaitTermination in ExecutorService “happens-before” any code executed after it?

一曲冷凌霜 提交于 2019-12-06 03:32:07
问题 Please, help to understand ExecutorService#awaitTermination(timeout) behaviour. I'm observing situation when I have in my code: private void shutdownAndAwaitTermination(ExecutorService threadPool){ threadPool.shutdown(); try { if (!threadPool.awaitTermination(threadPoolTimeout, TimeUnit.HOURS)){ threadPool.shutdownNow(); if (!threadPool.awaitTermination(60, TimeUnit.SECONDS)) { logger.warn("Pool did not terminate"); } } } catch (InterruptedException ie) { threadPool.shutdownNow(); Thread

newFixedThreadPool() vs newCachedThreadPool() [duplicate]

依然范特西╮ 提交于 2019-12-06 03:16:26
This question already has an answer here: Executors.newCachedThreadPool() versus Executors.newFixedThreadPool() 8 answers In case newCachedThreadPool() as per creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available whereas in case of newFixedThreadPool(int size) specify size to create the thread pool with size specified. Why isn't newFixedThreadPool(int size) implemented in newCachedThreadPool() fashion where thread pool creates the new thread only when required and will limit the thread to size? Any clarrification on the

ThreadPoolExecutor : : TaskRejectedException from Executor

岁酱吖の 提交于 2019-12-05 17:13:11
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.scheduling.concurrent.ThreadPoolTaskExecutor" scope="singleton" destroy-method="destroy"> <property name=

Do we need to shutdown ExecutorService fixedThreadPool

不羁的心 提交于 2019-12-05 15:35:32
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. Fabio Cardoso newFixedThreadPool public static ExecutorService newFixedThreadPool(int nThreads) Creates a thread pool that reuses a fixed

Java concurrency pattern for external shared resource (smartcards)

谁说我不能喝 提交于 2019-12-05 10:26:04
问题 I have a web server service where clients request a smartcard computation and get their result. Available smartcard number can decrease or increase during the server uptime, for example I can add or remove physically the smartcard from the reader (or many other events... like exception and so on). A smartcard computation can take a while, so I have to optimize these jobs to use all available smartcards if there are concurrent requests to the web server. I thought to work with a smartcard

LinkedBlockingQueue put vs offer

旧时模样 提交于 2019-12-04 23:19:05
I have a linked blocking queue in which I am performing insertion and removal operations. I need to know which one is better put or offer in case of linked blocking queue. Performance parameters are CPU utilization, memory and overall throughput. Application usage is Real time system where there can be multiple incoming requests and less threads to handle where we will need to insert element in queue. I read Java docs of put and offer there was not much difference in internal application. Batty Actually, you can't compare performance between these two, the offer method is to just offer to the

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

六眼飞鱼酱① 提交于 2019-12-04 18:06:17
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 the running thread to complete the remaining 40? Use ForkJoinPool A ForkJoinPool differs from other kinds

Does awaitTermination in ExecutorService “happens-before” any code executed after it?

可紊 提交于 2019-12-04 07:01:51
Please, help to understand ExecutorService#awaitTermination(timeout) behaviour. I'm observing situation when I have in my code: private void shutdownAndAwaitTermination(ExecutorService threadPool){ threadPool.shutdown(); try { if (!threadPool.awaitTermination(threadPoolTimeout, TimeUnit.HOURS)){ threadPool.shutdownNow(); if (!threadPool.awaitTermination(60, TimeUnit.SECONDS)) { logger.warn("Pool did not terminate"); } } } catch (InterruptedException ie) { threadPool.shutdownNow(); Thread.currentThread().interrupt(); } } Does the tasks in pool complete in this case before any other calls after

Process list of 'N' items with multiple threads

北战南征 提交于 2019-12-04 06:51:47
I have List of N items and I want to divide this List in a sequential manner between a fixed number of threads . By sequential I mean, I want to pass 1 to N/4 to first thread , N/4 + 1 to N/2 to second thread and N/2+1 to N to third thread , Now once all the threads have finished their work, I want to notify to main thread to send some message that all the processing has been completed. What I have done so far now is that I have implemented ExecutorService I did something like this ExecutorService threadPool = Executors.newFixedThreadPool(Number_of_threads); //List of items List List <items

get callable from ThreadPoolTaskExecutor or cast Runnable to Callable

亡梦爱人 提交于 2019-12-04 05:05:35
问题 I'm using ThreadPoolTaskExecutor for executing my tasks which are implemantations of Callable interface. I just want to check in time if task is still in pool (monitoring). How to do that? I know that I can get queue from ThreadPoolExecutor but how can I cast Runnable to Callable? Basically I have this callable public interface IFormatter extends Callable<Integer>{ Long getOrderId(); } I'm executing it like this ThreadPoolExecutor.submit(new Formatter(order)); And finally I'd like to loop