About the relationship between coroutines, threads and concurrency issues

« - », ? - ? . , Android, , , Java, .





, . Android, , , . , :





// ,   10-      
someScope.launch(Dispatchers.Default) {
   val fibonacci10 = synchronousFibonacci(10)
   saveFibonacciInMemory(10, fibonacci10)
}

private fun synchronousFibonacci(n: Long): Long { /* … */ }
      
      



(async



) , , (thread pool), , Dispatchers.Default



. - .





, , . , (dispatcher), , (yield)/ (suspend) t, .





:





//    4 
val executorService = Executors.newFixedThreadPool(4)

//          
executorService.execute {
   val fibonacci10 = synchronousFibonacci(10)
   saveFibonacciInMemory(10, fibonacci10)
}

      
      



, Android , , , Jetpack.





? , , CoroutineDispatcher ; Dispatchers.Default



.





CoroutineDispatcher . , CoroutineDispatcher



, , interceptContinuation, Continuation



( ) DispatchedContinuation. , CoroutineDispatcher



ContinuationInterceptor .





, , , , (, ) Continuation.





resumeWith DispatchedContinuation



, Continuation !





, DispatchedContinuation DispatchedTask, Java, , Runnable



. , DispatchedContinuation



! ? CoroutineDispatcher



, DispatchedTask



, Runnable



!





dispatch



? , , start



CoroutineStart. , , , CoroutineStart.LAZY



. CoroutineStart.DEFAULT



, CoroutineDispatcher



. !





An illustration of how a block of code in a coroutine is sent to the thread for execution
,

,





, CoroutineDispatcher



Executor.asCoroutineDispatcher(). (Dispatchers), .





, Dispatchers.Default



createDefaultDispatcher. DefaultScheduler. Dispatchers.IO, DefaultScheduler



64 . Dispatchers.Default



Dispatchers.IO



, , . withContext



?





withContext

Android , , . ! , , . , , . , - withContext



?





, , , , ( , ). , ! , , (suspend) ! !





CoroutineScheduler, Java , . Dispatchers.Default



Dispatchers.IO



, , , . , fast-path.





Dispatchers.Main



, Dispatchers.Default



Dispatchers.Main



, ( ) .





- , . , : Java, , . , , .





, , , , . , . - ! , .





. , , , . , , , , .





, Java. .





, , . , , , , , , . , . , , !





. , , . , ! , (suspend) withContext(defaultDispatcher)



!





, , . , , , , :





class TransactionsRepository(
  private val defaultDispatcher: CoroutineDispatcher = Dispatchers.Default
) {

  private val transactionsCache = mutableMapOf<User, List<Transaction>()

  private suspend fun addTransaction(user: User, transaction: Transaction) =
   // !     .
   //   :     
   //     .
    withContext(defaultDispatcher) {
      if (transactionsCache.contains(user)) {
        val oldList = transactionsCache[user]
        val newList = oldList!!.toMutableList()
        newList.add(transaction)
        transactionsCache.put(user, newList)
      } else {
        transactionsCache.put(user, listOf(transaction))
      }
    }
}
      
      



Kotlin, « Java » - , Java. , Jetbrains .





, . , , , API . , API, , .





. , .





/ . / (producer/consumer) . JetBrains .





Android , . , AtomicInteger. , map , ConcurrentHashMap. ConcurrentHashMap



- , map.





, , . , . , transactionCache



, , .





, , .





, , @Volatile ! , @Synchronized , .





, (latches), (semaphores) (barriers). (locks) (mutexes).





Mutex lock unlock, . , Mutex.withLock :





class TransactionsRepository(
  private val defaultDispatcher: CoroutineDispatcher = Dispatchers.Default
) {

  // ,    
  private val cacheMutex = Mutex()
  private val transactionsCache = mutableMapOf<User, List<Transaction>()

  private suspend fun addTransaction(user: User, transaction: Transaction) =
    withContext(defaultDispatcher) {
      // ,    
      cacheMutex.withLock {
        if (transactionsCache.contains(user)) {
          val oldList = transactionsCache[user]
          val newList = oldList!!.toMutableList()
          newList.add(transaction)
          transactionsCache.put(user, newList)
        } else {
          transactionsCache.put(user, listOf(transaction))
        }
      }
    }
}
      
      



, , , Mutex



, , Java, . Java , , , .





, , . , Android . . !






Android Developer. Basic.





- : " . Room"





Android Developer. Professional








All Articles