Last active
May 21, 2018 12:37
-
-
Save amitshekhariitbhu/c90e5a5b07f7a18a9b1bc2f8fd0b265b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Singleton class for default executor supplier | |
*/ | |
public class DefaultExecutorSupplier{ | |
/* | |
* Number of cores to decide the number of threads | |
*/ | |
public static final int NUMBER_OF_CORES = Runtime.getRuntime().availableProcessors(); | |
/* | |
* thread pool executor for background tasks | |
*/ | |
private final ThreadPoolExecutor mForBackgroundTasks; | |
/* | |
* thread pool executor for light weight background tasks | |
*/ | |
private final ThreadPoolExecutor mForLightWeightBackgroundTasks; | |
/* | |
* thread pool executor for main thread tasks | |
*/ | |
private final Executor mMainThreadExecutor; | |
/* | |
* an instance of DefaultExecutorSupplier | |
*/ | |
private static DefaultExecutorSupplier sInstance; | |
/* | |
* returns the instance of DefaultExecutorSupplier | |
*/ | |
public static DefaultExecutorSupplier getInstance() { | |
if (sInstance == null) { | |
synchronized(DefaultExecutorSupplier.class){ | |
sInstance = new DefaultExecutorSupplier(); | |
} | |
return sInstance; | |
} | |
/* | |
* constructor for DefaultExecutorSupplier | |
*/ | |
private DefaultExecutorSupplier() { | |
// setting the thread factory | |
ThreadFactory backgroundPriorityThreadFactory = new | |
PriorityThreadFactory(Process.THREAD_PRIORITY_BACKGROUND); | |
// setting the thread pool executor for mForBackgroundTasks; | |
mForBackgroundTasks = new ThreadPoolExecutor( | |
NUMBER_OF_CORES * 2, | |
NUMBER_OF_CORES * 2, | |
60L, | |
TimeUnit.SECONDS, | |
new LinkedBlockingQueue<Runnable>(), | |
backgroundPriorityThreadFactory | |
); | |
// setting the thread pool executor for mForLightWeightBackgroundTasks; | |
mForLightWeightBackgroundTasks = new ThreadPoolExecutor( | |
NUMBER_OF_CORES * 2, | |
NUMBER_OF_CORES * 2, | |
60L, | |
TimeUnit.SECONDS, | |
new LinkedBlockingQueue<Runnable>(), | |
backgroundPriorityThreadFactory | |
); | |
// setting the thread pool executor for mMainThreadExecutor; | |
mMainThreadExecutor = new MainThreadExecutor(); | |
} | |
/* | |
* returns the thread pool executor for background task | |
*/ | |
public ThreadPoolExecutor forBackgroundTasks() { | |
return mForBackgroundTasks; | |
} | |
/* | |
* returns the thread pool executor for light weight background task | |
*/ | |
public ThreadPoolExecutor forLightWeightBackgroundTasks() { | |
return mForLightWeightBackgroundTasks; | |
} | |
/* | |
* returns the thread pool executor for main thread task | |
*/ | |
public Executor forMainThreadTasks() { | |
return mMainThreadExecutor; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sorry to bother you, but how can I test this to prove it actually works?