mirror of
https://github.com/SoPat712/TrackCovid19.git
synced 2025-08-27 21:02:18 -04:00
Initial Upload
This commit is contained in:
64
app/src/main/java/com/josh/trackcovid19v2/AppExecutors.java
Executable file
64
app/src/main/java/com/josh/trackcovid19v2/AppExecutors.java
Executable file
@@ -0,0 +1,64 @@
|
||||
package com.josh.trackcovid19v2;
|
||||
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
/**
|
||||
* Global executor pools for the whole application.
|
||||
* <p>
|
||||
* Grouping tasks like this avoids the effects of task starvation (e.g. disk reads don't wait behind
|
||||
* webservice requests).
|
||||
* <p>
|
||||
* Creating a class like this is a way for android apps to handle non-trivial threading.
|
||||
*/
|
||||
public class AppExecutors {
|
||||
|
||||
// For Singleton instantiation
|
||||
private static final Object LOCK = new Object();
|
||||
private static AppExecutors sInstance;
|
||||
private final Executor diskIO;
|
||||
private final Executor mainThread;
|
||||
private final Executor networkIO;
|
||||
|
||||
private AppExecutors(Executor diskIO, Executor networkIO, Executor mainThread) {
|
||||
this.diskIO = diskIO;
|
||||
this.networkIO = networkIO;
|
||||
this.mainThread = mainThread;
|
||||
}
|
||||
|
||||
public static AppExecutors getInstance() {
|
||||
if (sInstance == null) {
|
||||
synchronized (LOCK) {
|
||||
sInstance = new AppExecutors(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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user