What is the use of allowCoreThreadTimeout () in ThreadPoolExecutor?

I need to use Java ThreadPoolExecutor in one of my Android components. I was looking for using allowCoreThreadTimeout ().

I read the related Java and Android docs. But I did not have any useful method implementation scenario. Can someone please help me?

+7
java android threadpoolexecutor
source share
5 answers

This method allows you to specify whether the main thread should be completed if there is no incoming task in the thread for a long time. This is due to another configuration, for example, setCorePoolSize (), setKeepAliveTime (..)

When you create a thread pool and threads exist in the pool, even if the task is not running. It is worth keeping these threads alive. If you want to get them, if you do not have a task to perform this method, it is useful in this case. You need to convey the true value, after which they will die after saving time.

In summary:

allowCoreThreadTimeOut(true) // Could save memory compromising performance allowCoreThreadTimeOut(false) // Comsume memory but high performance 
+7
source share
 public void allowCoreThreadTimeOut(boolean value) 

This is explained in javadoc

Sets a policy that determines whether the main threads can time out and stop working if no tasks appear during the save time, replacing them if necessary when new tasks appear.

When false, the main threads are never interrupted due to the lack of incoming tasks. When true, the same keep-alive policy that applies to non-primary threads also applies to main threads. To avoid constant thread replacement, the save time is kept when true is set. This method must be called before the pool is actively used.

+2
source share

Well allowCoreThreadTimeout (true) should be used when you want to terminate kernel pool threads after a specific timeout.

Please refer to the following link from the documentation:

Android API Docs - ThreadPoolExecutor

If you are really in low memory, you must pass true to this method.

0
source share

you can check the parsing for android sdk, it is really nice.

0
source share

Allowing the main threads to time out allows the application to efficiently handle the "packet" traffic. Consider the scenario when a corporate application does not work during business hours, but receives a packet of requests at the end of the day.

One way to effectively manage this scenario would be to enable allowCoreThreadTimeout() and set coreThreads = maxThreads to some corresponding high value. During this peak time, your thread pool will scale to handle traffic, and then decrease to zero, freeing up server resources.

0
source share

All Articles