Guest User

Untitled

a guest
Feb 17th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. val exe = AppExecutors
  2. exe.util {
  3. val first = calculateFirst()
  4. val second = calculateSecond()
  5. val str = ("first = $first | second = $second")
  6. exe.main {
  7. Toast.makeText(activity, "Executors $str", Toast.LENGTH_LONG).show()
  8. }
  9. }
  10.  
  11. object AppCoRoutines{
  12. private val uiContext: CoroutineContext = Dispatchers.Main
  13. private val ioContext: CoroutineContext = Dispatchers.IO
  14. private val networkContext: CoroutineContext = Executors.newFixedThreadPool(3).asCoroutineDispatcher()
  15. private val singleContext: CoroutineContext = Executors.newSingleThreadExecutor().asCoroutineDispatcher()
  16.  
  17. val ui: CoroutineScope = CoroutineScope(uiContext)
  18. val io: CoroutineScope = CoroutineScope(ioContext)
  19. val net: CoroutineScope = CoroutineScope(networkContext)
  20. val single: CoroutineScope = CoroutineScope(singleContext)
  21. }
  22.  
  23. val coRout = AppCoRoutines
  24. coRout.ui.launch {
  25. val str: String = withContext(coRout.net.coroutineContext){
  26. val first = async { calculateFirst() }
  27. val second = async { calculateSecond() }
  28. ("first = $first | second = $second")
  29. }
  30. Toast.makeText(activity, "CoRoutine $str", Toast.LENGTH_LONG).show()
  31. }
Add Comment
Please, Sign In to add comment