Skip to content

Instantly share code, notes, and snippets.

@codejunk1e
Created June 20, 2020 18:25
Show Gist options
  • Save codejunk1e/3cba6718da66311ae350fe49b2c894c8 to your computer and use it in GitHub Desktop.
Save codejunk1e/3cba6718da66311ae350fe49b2c894c8 to your computer and use it in GitHub Desktop.
Executors file template for Android
/**
* @author ${USER}
* Created on ${DATE}
*/
import android.os.Handler;
import android.os.Looper;
import androidx.annotation.NonNull;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
public class ${NAME} {
// For Singleton instantiation
private static final Object LOCK = new Object();
private static ${NAME} sInstance;
private final Executor diskIO;
private final Executor mainThread;
private final Executor networkIO;
private ${NAME}(Executor diskIO, Executor networkIO, Executor mainThread) {
this.diskIO = diskIO;
this.networkIO = networkIO;
this.mainThread = mainThread;
}
public static ${NAME} getInstance() {
if (sInstance == null) {
synchronized (LOCK) {
sInstance = new ${NAME}(Executors.newSingleThreadExecutor(),
Executors.newFixedThreadPool(3),
new MainThreadExecutor());
}
}
return sInstance;
}
public Executor diskIO() {
return diskIO;
}
public Executor mainThread() {
return mainThread;
}
public Executor networkIO() {
return networkIO;
}
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