Created
October 21, 2020 09:36
-
-
Save nadar71/61a671674cbdeab26566e84e54ca35f8 to your computer and use it in GitHub Desktop.
class for executors
This file contains hidden or 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
import android.os.Handler | |
import android.os.Looper | |
import java.util.concurrent.Executor | |
import java.util.concurrent.Executors | |
// Global executor pools. | |
class AppExecutors // singleton constructor | |
private constructor(private val diskIO: Executor, private val mainThread: Executor, | |
private val networkIO: Executor) { | |
fun diskIO(): Executor { | |
return diskIO | |
} | |
fun mainThread(): Executor { | |
return mainThread | |
} | |
fun networkIO(): Executor { | |
return networkIO | |
} | |
private class MainThreadExecutor : Executor { | |
private val mainThreadHandler = Handler(Looper.getMainLooper()) | |
override fun execute(command: Runnable) { | |
mainThreadHandler.post(command) | |
} | |
} | |
companion object { | |
// singleton instantiation | |
@Volatile private var singleInstance: AppExecutors? = null | |
val instance: AppExecutors | |
get(){ | |
return singleInstance ?: synchronized(this) { | |
AppExecutors( | |
Executors.newSingleThreadExecutor(), | |
Executors.newFixedThreadPool(3), | |
MainThreadExecutor() | |
) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment