12

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??

Somesh Gupta
  • 277
  • 1
  • 4
  • 20
  • 1
    I suppose if one could imagine a scenario where having less idle threads is beneficial, then that would be a use case. Perhaps if you have a very large thread pool (to handle a worst case scenario), but you don't wish to keep 100s of threads alive at all times. – Duncan Jones Aug 14 '13 at 07:43

5 Answers5

12

This method allows you to specify whether to terminate the core thread if there is no incoming task within the thread keep alive time. This is related to other configuration like, setCorePoolSize(), setKeepAliveTime(..)

When you are creating a thread pool and idle threads exist in the pool even though there is no task is running. It is costly to keep these thread alive. If you want get rid of these when you have no task to execute, this method is useful. You need to pass true value then they will be die after the keep alive time.

In Summary:

allowCoreThreadTimeOut(true) // Could save memory compromising performance

allowCoreThreadTimeOut(false) // Comsume memory but high performance 
Nino Walker
  • 2,742
  • 22
  • 30
Shamim Ahmmed
  • 8,265
  • 6
  • 25
  • 36
3

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.

Andy Brown
  • 11,766
  • 2
  • 42
  • 61
2
public void allowCoreThreadTimeOut(boolean value)

This is greatly explained in javadoc

Sets the policy governing whether core threads may time out and terminate if no tasks arrive within the keep-alive time, being replaced if needed when new tasks arrive.

When false, core threads are never terminated due to lack of incoming tasks. When true, the same keep-alive policy applying to non-core threads applies also to core threads. To avoid continual thread replacement, the keep-alive time must be greater than zero when setting true. This method should in general be called before the pool is actively used.

Community
  • 1
  • 1
Tala
  • 8,888
  • 5
  • 34
  • 38
  • 2
    I think the OP is asking for scenarios in which you might *want* to do this. – Duncan Jones Aug 14 '13 at 07:43
  • @Tala I allready mentioned that I have read the docs. The problem is I don't understand how to implement it. Also could you please put some light on "being replaced if needed when new tasks arrive". – Somesh Gupta Aug 14 '13 at 07:45
1

It's useful in situations when you can not call ThreadPoolExecutor.shutdown method explicitly at the end of your object lifecycle (framework doesn't provide "onClose" hook, for example), but you need to use ThreadPoolExecutor. In this case, without allowCoreThreadTimeout(true) method call, ThreadPoolExecutor's core threads will block GC of your object and cause memory leaks.

Here's how this scenario referenced in "Finalization" section of the ThreadPoolExecutor documentation:

A pool that is no longer referenced in a program AND has no remaining threads will be shutdown automatically. If you would like to ensure that unreferenced pools are reclaimed even if users forget to call shutdown(), then you must arrange that unused threads eventually die, by setting appropriate keep-alive times, using a lower bound of zero core threads and/or setting allowCoreThreadTimeOut(boolean).

Igor Dvorzhak
  • 4,360
  • 3
  • 17
  • 31
-2

you can check parse implementation for android sdk, it's really nice.

someUser
  • 965
  • 12
  • 24