Skip to content

Instantly share code, notes, and snippets.

@JcMinarro
Created March 19, 2015 13:01
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save JcMinarro/a1cefdbb81005696a931 to your computer and use it in GitHub Desktop.
ThreadExecutorExample
/**
* @author Jc Miñarro
*/
public interface Executor<T extends Runnable> {
void run(final T interactor);
}
/**
* @author Jc Miñarro
*/
public interface MainThread {
void post(final Runnable runnable);
}
import android.os.Handler;
import android.os.Looper;
/**
* @author Jc Miñarro
*/
public class MainThreadImpl implements MainThread {
private final Handler handler;
public MainThreadImpl() {
handler = new Handler(Looper.getMainLooper());
}
@Override
public void post(Runnable runnable) {
handler.post(runnable);
}
import java.util.Comparator;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* Executor implementation based on ThreadPoolExecutor. ThreadPoolExecutorConfig:
*
* Core pool size: 3.
* Max pool size: 5.
* Keep alive time: 120.
* Time unit: seconds.
* Work queue: LinkedBlockingQueue.
*
* @author Jc Miñarro
*/
public class ThreadExecutor implements Executor<ThreadExecutor.PriorityRunnable> {
private static final int CORE_POOL_SIZE = 3;
private static final int MAX_POOL_SIZE = 5;
private static final int KEEP_ALIVE_TIME = 120;
private static final TimeUnit TIME_UNIT = TimeUnit.SECONDS;
private ThreadPoolExecutor threadPoolExecutor;
public ThreadExecutor() {
int corePoolSize = CORE_POOL_SIZE;
int maxPoolSize = MAX_POOL_SIZE;
int keepAliveTime = KEEP_ALIVE_TIME;
TimeUnit timeUnit = TIME_UNIT;
BlockingQueue workQueue = new PriorityBlockingQueue<PriorityRunnable>(corePoolSize, new Comparator<PriorityRunnable>() {
@Override
public int compare(PriorityRunnable lhs, PriorityRunnable rhs) {
return rhs.getPriority().compareTo(lhs.getPriority());
}
});
threadPoolExecutor =
new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, timeUnit, workQueue);
}
@Override
public void run(final PriorityRunnable interactor) {
if (interactor == null) {
throw new IllegalArgumentException("Interactor to execute can't be null");
}
threadPoolExecutor.execute(interactor);
}
public interface PriorityRunnable extends Runnable {
public enum Priority {
LOW,
HI
}
public Priority getPriority();
}
}
/**
* @author Jc Miñarro
*/
public class ThreadFacade {
private final MainThread mainThread;
private final Executor executor;
public ThreadFacade(MainThread mainThread, Executor executor) {
this.mainThread = mainThread;
this.executor = executor;
}
public void postOnBackgroundWithHiPriority(Runnable runnable) {
this.executor.run(new HiPriorityRunnable(runnable));
}
public void postOnBackgroundWithLowPriority(Runnable runnable) {
this.executor.run(new LowPriorityRunnable(runnable));
}
public void postOnMainThread(Runnable runnable) {
this.mainThread.post(runnable);
}
private abstract class PriorityRunnableAbs implements ThreadExecutor.PriorityRunnable {
protected final Runnable runnable;
public PriorityRunnableAbs(Runnable runnable) {
this.runnable = runnable;
}
@Override
public void run() {
this.runnable.run();
}
}
private class HiPriorityRunnable extends PriorityRunnableAbs{
public HiPriorityRunnable(Runnable runnable) {
super(runnable);
}
@Override
public Priority getPriority() {
return Priority.HI;
}
}
private class LowPriorityRunnable extends PriorityRunnableAbs{
public LowPriorityRunnable(Runnable runnable) {
super(runnable);
}
@Override
public Priority getPriority() {
return Priority.LOW;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment