Skip to content

Instantly share code, notes, and snippets.

@grantland
Last active March 23, 2016 08:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grantland/7489737 to your computer and use it in GitHub Desktop.
Save grantland/7489737 to your computer and use it in GitHub Desktop.
import android.annotation.TargetApi;
import android.os.Build;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* Created by Grantland Chew on 11/14/13.
*
* This class exists because java.util.concurrent.Executors is terrible.
*
* ThreadPoolExecutor - A thread pool that keeps corePoolSize threads alive at all times. Once all
* corePoolSize threads are in use, it will then queue operations in workQueue. Once workQueue is
* full, it will then create more threads until maxPoolSize. Once maxPoolSize is full, it will
* drop operations. Problems: corePoolSize threads will be alive at all times, it will not grow
* thread pool until queue is full, it will not use a queue when maxPoolSize is full.
*
* ScheduledThreadPoolExecutor - A thread pool that extends ThreadPoolExecutor so it works just
* X corePoolSize, Integer.MAX_VALUE maxPoolSize, 0L timeout, DelayedWorkQueue workQueue. Problems:
* unable to schedule operations if pool size is ever 0.
*
* Executors.newCachedThreadPool() - Returns a ThreadPoolExecutor with 0 corePoolSize,
* Integer.MAX_VALUE maxPoolSize, 60L timeout, SynchronousQueue workQueue. Any operation will be
* executed on a new or cached thread immediately because corePoolSize is 0 and SynchronousQueue
* is a queue with size 0. Terrible because it can create an unchecked amount of threads.
*
* Executors.newFixedThreadPool(int nThreads) - Returns a ThreadPoolExecutor with nThreads
* corePoolSize, nThreads maxPoolSize, 0L timeout, LinkedBlockingQueue workQueue.
*
* Executors.newScheduledThreadPool(int corePoolSize) - Returns a ScheduledThreadPoolExecutor with
* corePoolSize corePoolSize.
*/
public class Executors {
/**
* Creates a thread pool that creates new threads as needed, but
* will reuse previously constructed threads when they are
* available. These pools will typically improve the performance
* of programs that execute many short-lived asynchronous tasks.
* Calls to {@code execute} will reuse previously constructed
* threads if available. If no existing thread is available, a new
* thread will be created and added to the pool. Threads that have
* not been used for ten seconds are terminated and removed from
* the cache. Thus, a pool that remains idle for long enough will
* not consume any resources. Note that pools with similar
* properties but different details (for example, timeout parameters)
* may be created using {@link ThreadPoolExecutor} constructors.
*
* @return the newly created thread pool
*/
public static ExecutorService newCachedThreadPool(int nThreads, int maximumQueueSize) {
ThreadPoolExecutor executor = new ThreadPoolExecutor(nThreads, nThreads,
10L, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(maximumQueueSize));
// This will allow the number of threads in the ThreadPoolExecutor to go down to 0.
// ThreadPoolExecutor usually doesn't timeout the core pool.
// ThreadPoolExecutor#allowCoreThreadTimeOut is only available in android-9
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
executor.allowCoreThreadTimeOut(true);
}
return executor;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment