Created
June 20, 2020 18:25
-
-
Save codejunk1e/3cba6718da66311ae350fe49b2c894c8 to your computer and use it in GitHub Desktop.
Executors file template for Android
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
/** | |
* @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