Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of allowCoreThreadTimeout( ) in ThreadPoolExecutor?

I have to use Java ThreadPoolExecutor in one of my component in Android. I was searching the use of allowCoreThreadTimeout( ).

I have read the Java & Android docs related. But I didn't get any useful implementation scenario of the method. Can someone please help me??

like image 434
Somesh Gupta Avatar asked Aug 14 '13 07:08

Somesh Gupta


People also ask

What is ExecutorService how it is used to create pool of threads?

The ExecutorService interface contains a large number of methods to control the progress of the tasks and manage the termination of the service. Using this interface, we can submit the tasks for execution and also control their execution using the returned Future instance.

What is the use of ThreadPoolExecutor?

ThreadPoolExecutor is an ExecutorService to execute each submitted task using one of possibly several pooled threads, normally configured using Executors factory methods. It also provides various utility methods to check current threads statistics and control them.

What is CorePoolSize ThreadPoolExecutor?

CorePoolSize: The ThreadPoolExecutor has an attribute corePoolSize that determines how many threads it will start until new threads are only started when the queue is full. MaximumPoolSize: This attribute determines how many threads are started at the maximum. You can set this to Integer.

How do you prevent ThreadPoolExecutor?

Call cancel() on the Future to Cancel a Task You can cancel tasks submitted to the ThreadPoolExecutor by calling the cancel() function on the Future object. Recall that you will receive a Future object when you submit your task to the thread pool by calling the submit() function.


1 Answers

Permitting core threads to timeout allows an application to efficiently handle 'bursty' traffic. Consider a scenario where an enterprise application is idle during business hours but receives a burst of many requests at the end of the day.

One way of efficiently handling this scenario would be to enable allowCoreThreadTimeout() and set coreThreads = maxThreads to some appropriately high value. During that peak time your thread pool would scale up to handle the traffic and then afterwards scale back down to zero, freeing up server resources.

like image 75
Andy Brown Avatar answered Oct 06 '22 00:10

Andy Brown