PriorityThreadFactory.java
import java.util.concurrent.ThreadFactory;
class PriorityThreadFactory implements ThreadFactory {
private final int mThreadPriority;
public PriorityThreadFactory(int threadPriority) {
mThreadPriority = threadPriority;
}
@Override
public Thread newThread(final Runnable runnable) {
Runnable wrapperRunnable = new Runnable() {
@Override
public void run() {
try {
android.os.Process.setThreadPriority(mThreadPriority);
} catch (Throwable t) {
}
runnable.run();
}
};
return new Thread(wrapperRunnable);
}
}
MainThreadExecutor.java
import android.os.Handler;
import android.os.Looper;
import java.util.concurrent.Executor;
class MainThreadExecutor implements Executor {
private final Handler handler = new Handler(Looper.getMainLooper());
@Override
public void execute(Runnable runnable) {
handler.post(runnable);
}
}
DefaultExecutorSupplier.java
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class DefaultExecutorSupplier {
public static final int NUMBER_OF_CORES = Runtime.getRuntime().availableProcessors();
private final ThreadPoolExecutor mForBackgroundTasks;
private final ThreadPoolExecutor mForLightWeightBackgroundTasks;
private final Executor mMainThreadExecutor;
private static DefaultExecutorSupplier sInstance;
public static DefaultExecutorSupplier getInstance() {
if (sInstance == null) {
synchronized (DefaultExecutorSupplier.class) {
sInstance = new DefaultExecutorSupplier();
}
}
return sInstance;
}
private DefaultExecutorSupplier() {
ThreadFactory backgroundPriorityThreadFactory = new PriorityThreadFactory(android.os.Process.THREAD_PRIORITY_BACKGROUND);
mForBackgroundTasks = new ThreadPoolExecutor(
NUMBER_OF_CORES * 2,
NUMBER_OF_CORES * 2,
60L,
TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(),
backgroundPriorityThreadFactory
);
mForLightWeightBackgroundTasks = new ThreadPoolExecutor(
NUMBER_OF_CORES * 2,
NUMBER_OF_CORES * 2,
60L,
TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(),
backgroundPriorityThreadFactory
);
mMainThreadExecutor = new MainThreadExecutor();
}
public ThreadPoolExecutor forBackgroundTasks() {
return mForBackgroundTasks;
}
public ThreadPoolExecutor forLightWeightBackgroundTasks() {
return mForLightWeightBackgroundTasks;
}
public Executor forMainThreadTasks() {
return mMainThreadExecutor;
}
//demo
public void doSomeBackgroundWork() {
DefaultExecutorSupplier.getInstance().forBackgroundTasks()
.execute(new Runnable() {
@Override
public void run() {
// do some background work here.
}
});
}
//demo
public void doSomeLightWeightBackgroundWork() {
DefaultExecutorSupplier.getInstance().forLightWeightBackgroundTasks()
.execute(new Runnable() {
@Override
public void run() {
// do some light-weight background work here.
}
});
}
//demo
public void doSomeMainThreadWork() {
DefaultExecutorSupplier.getInstance().forMainThreadTasks()
.execute(new Runnable() {
@Override
public void run() {
// do some Main Thread work here.
}
});
}
}