Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- val exe = AppExecutors
- exe.util {
- val first = calculateFirst()
- val second = calculateSecond()
- val str = ("first = $first | second = $second")
- exe.main {
- Toast.makeText(activity, "Executors $str", Toast.LENGTH_LONG).show()
- }
- }
- object AppCoRoutines{
- private val uiContext: CoroutineContext = Dispatchers.Main
- private val ioContext: CoroutineContext = Dispatchers.IO
- private val networkContext: CoroutineContext = Executors.newFixedThreadPool(3).asCoroutineDispatcher()
- private val singleContext: CoroutineContext = Executors.newSingleThreadExecutor().asCoroutineDispatcher()
- val ui: CoroutineScope = CoroutineScope(uiContext)
- val io: CoroutineScope = CoroutineScope(ioContext)
- val net: CoroutineScope = CoroutineScope(networkContext)
- val single: CoroutineScope = CoroutineScope(singleContext)
- }
- val coRout = AppCoRoutines
- coRout.ui.launch {
- val str: String = withContext(coRout.net.coroutineContext){
- val first = async { calculateFirst() }
- val second = async { calculateSecond() }
- ("first = $first | second = $second")
- }
- Toast.makeText(activity, "CoRoutine $str", Toast.LENGTH_LONG).show()
- }
Add Comment
Please, Sign In to add comment