Created
March 7, 2018 00:15
-
-
Save micer/e45307a2c46eb4cadf1cbe4a0d1168e7 to your computer and use it in GitHub Desktop.
Elegant helper class to run operations on different threads.
This file contains 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
package eu.micer.sample.util; | |
import android.os.Handler; | |
import android.os.Looper; | |
import android.support.annotation.NonNull; | |
import java.util.concurrent.Executor; | |
import java.util.concurrent.Executors; | |
public class AppExecutors { | |
private final Executor diskIO; | |
private final Executor networkIO; | |
private final Executor mainThread; | |
private AppExecutors(Executor diskIO, Executor networkIO, Executor mainThread) { | |
this.diskIO = diskIO; | |
this.networkIO = networkIO; | |
this.mainThread = mainThread; | |
} | |
public AppExecutors() { | |
this(Executors.newSingleThreadExecutor(), Executors.newFixedThreadPool(3), | |
new MainThreadExecutor()); | |
} | |
public Executor diskIO() { | |
return diskIO; | |
} | |
public Executor networkIO() { | |
return networkIO; | |
} | |
public Executor mainThread() { | |
return mainThread; | |
} | |
private static class MainThreadExecutor implements Executor { | |
private Handler mainThreadHandler = new Handler(Looper.getMainLooper()); | |
@Override | |
public void execute(@NonNull Runnable command) { | |
mainThreadHandler.post(command); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment