Title: Theory / Unimportant / CS / Programming / Kotlin / Library / Coroutine
Tags: automatedNewLineAfterCommaSpace

Instruction: coroutine
Page: https://kotlinlang.org/docs/coroutines-overview.html#documentation
Effect: todo
Language: Kotlin

Instruction: delay(ms)
Page: https://kotlinlang.org/docs/coroutines-basics.html#your-first-coroutine
Effect: do nothing in current coroutine during ms miliseconds
Language: kotlin
Context: Coroutine

Instruction: launch {foo}
Page: https://kotlinlang.org/docs/coroutines-basics.html#your-first-coroutine
Effect: run foo concurrently with the rest of the code
Language: kotlin
Context: Coroutine
Where to use it: coroutine scope

Instruction: runBlocking {foo}
Page: https://kotlinlang.org/docs/coroutines-basics.html#your-first-coroutine
Effect: Execute foo in a new coroutine scope
Language: kotlin
Context: Coroutine
Where to use it: outside of coroutine scope

Name: Structured concurrency
Definition: Principles where new coroutines can be only launched in a specific CoroutineScope which delimits the lifetime of the coroutine
Page: https://kotlinlang.org/docs/coroutines-basics.html#your-first-coroutine
Chapter: kotlin
Context: Coroutine

Name: Coroutine scope
Definition: A part of the runtime where coroutine can be executed
Page: https://kotlinlang.org/docs/coroutines-basics.html#your-first-coroutine
Chapter: kotlin
Context: Coroutine

Instruction: suspend fun foo(..) {bar}
Page: https://kotlinlang.org/docs/coroutines-basics.html#your-first-coroutine
Meaning: declare a function that can run in coroutine scope.
Language: Kotlin
Context: Coroutine
Name: Suspending function
Similar to: fun foo(..) {bar}

Instruction: coroutineScope {
  foo
}
Page: https://kotlinlang.org/docs/coroutines-basics.html#your-first-coroutine
Effect: execute foo, blocks until its entirely done
Language: Kotlin
Context: Coroutine
Name: Suspending function

Typ: Job
Meaning: a handle to the launched coroutine
Page: https://kotlinlang.org/docs/coroutines-basics.html#your-first-coroutine
Language: Kotlin
Context: Coroutine

Instruction: job.join()
Page: https://kotlinlang.org/docs/coroutines-basics.html#your-first-coroutine
Effect: wait until job completes
Language: Kotlin
Context: Coroutine
Variables: Job job

Instruction: job.cancel()
Page: https://kotlinlang.org/docs/coroutines-basics.html#your-first-coroutine
Effect: cancel the job
Language: Kotlin
Context: Coroutine
Variables: Job job

Question: What to do after cancel a job
Answer: join it to wait until cancellation is done
Page: https://kotlinlang.org/docs/coroutines-basics.html#your-first-coroutine
Chapter: Kotlin
Variables: Job job
Context: Coroutine

Name: A cancellable function
Definition: A function that check whether they are cancelled and then throw CancellationException
Page: https://kotlinlang.org/docs/coroutines-basics.html#your-first-coroutine
Chapter: Kotlin
Variables: Job job
Context: Coroutine
Example: all ktolinx.coroutine method

Typ: CancellationException
Meaning: Exception thrown when the thread is cancelled
Page: https://kotlinlang.org/docs/coroutines-basics.html#your-first-coroutine
Language: Kotlin
Context: Coroutine
Variables: Job job

Instruction: isActive
Page: https://kotlinlang.org/docs/coroutines-basics.html#your-first-coroutine
Effect: Whether current coroutine is not cancelled
Language: kotlin
Context: Coroutine
Variables: Job job

Page: https://kotlinlang.org/docs/coroutines-basics.html#your-first-coroutine
Syntactic sugar: job.cancelAndJoin()
Implementation: job.cancel()
job.join()
Language: kotlin
Variables: Job job
Context: Coroutine

Instruction: withTimeout(
  ms,
  fn
)
Page: https://kotlinlang.org/docs/coroutines-basics.html#your-first-coroutine
Effect: run fn during at most ms miliseconds, then cancel
Language: kotlin
Context: Coroutine
Returns: result of fn.execute(  )
Variables: Job job
Exception: TimeoutCancellationException if the computation halt due to timeout

Instruction: withTimeoutOrNull(
  ms,
  fn
)
Page: https://kotlinlang.org/docs/coroutines-basics.html#your-first-coroutine
Effect: run fn during at most ms miliseconds, then cancel and catch the TimeoutCancellationException
Language: kotlin
Context: Coroutine
Returns: result of fn.execute(  ) or null
Variables: Job job
Exception: TimeoutCancellationException if the computation halt due to timeout

Typ: Deferred
Meaning: A job that returns some value at ends of its computation, with await
Page: https://kotlinlang.org/docs/composing-suspending-functions.html#concurrent-using-async
Language: kotlin
Context: Coroutine
Variables: Job job
Subtype of: Job

Instruction: await(fn)
Page: https://kotlinlang.org/docs/composing-suspending-functions.html#concurrent-using-async
Language: kotlin
Context: Coroutine
Returns: A Deferred, whose value is the result of fn()
Typ: Deferred

Instruction: deferred.await()
Page: https://kotlinlang.org/docs/composing-suspending-functions.html#concurrent-using-async
Effect: wait until the result is available()
Language: kotlin
Context: Coroutine
Returns: the result of the deferred
Variables: Deferred deferred

Instruction: async(
  start = CoroutineStart.LAZY,
  fn
)
Page: https://kotlinlang.org/docs/composing-suspending-functions.html#concurrent-using-async
Language: kotlin
Context: Coroutine
Returns: A deferred for method fn, that waits until await or start is called to start the job
Variables: Deferred deferred

Instruction: job.start()
Page: https://kotlinlang.org/docs/composing-suspending-functions.html#concurrent-using-async
Effect: start the job
Language: kotlin
Context: Coroutine
Variables: Job job

Instruction: @OptIn(DelicateCoroutinesApi::class)
Page: https://kotlinlang.org/docs/composing-suspending-functions.html#concurrent-using-async
Effect: allow to use GlobalScope
Language: kotlin
Context: Coroutine
Variables: Job job

Instruction: GlobalScope.async(fn)
Page: https://kotlinlang.org/docs/composing-suspending-functions.html#concurrent-using-async
Effect: Execute fn in an async way in the global scope
Language: kotlin
Context: Coroutine
Variables: Job job
Assuming: @OptIn(DelicateCoroutinesApi::class)

Instruction: measureTimeMillis(fn)
Page: https://kotlinlang.org/docs/composing-suspending-functions.html#concurrent-using-async
Effect: execute fn
Language: kotlin
Context: Coroutine
Returns: how much milisecond fn took

Question: What occurs if a scope throw an exception that is not catched
Answer: all child coroutine are cancelled
Page: https://kotlinlang.org/docs/composing-suspending-functions.html#concurrent-using-async
Chapter: kotlin
Context: Coroutine
Answer2: the exception is thrown from the block, except for cancel exception

Typ: CoroutineDispatcher
Meaning: Class in charge of determining the set of threads and queuing mechanism used by a coroutine context
Page: https://kotlinlang.org/docs/coroutine-context-and-dispatchers.html
Language: Kotlin
Chapter: kotlin
Context: Coroutine

Instruction: newSingleThreadContext("name")
Page: https://kotlinlang.org/docs/coroutine-context-and-dispatchers.html
Effect: A thread named name for coroutine
Language: Kotlin
Context: Coroutine
Chapter: kotlin

Instruction: Dispatchers.Default
Page: https://kotlinlang.org/docs/coroutine-context-and-dispatchers.html
Language: Kotlin
Context: Coroutine
Chapter: kotlin
Returns: default coroutine dispatcher

Instruction: Dispatchers.Unconfined
Page: https://kotlinlang.org/docs/coroutine-context-and-dispatchers.html
Language: Kotlin
Context: Coroutine
Chapter: kotlin
Returns: coroutine dispatcher that appears to run in main until first suspension point, then in the thread of the suspension point
Assuming: operation that don't consume CPU and don't update any shared data

Instruction: corountineContext[Job]
Page: https://kotlinlang.org/docs/coroutine-context-and-dispatchers.html
Language: Kotlin
Context: Coroutine
Chapter: kotlin
Returns: The job currently being executed

Name: A child of job
Definition: A coroutine started in job
Page: https://kotlinlang.org/docs/coroutine-context-and-dispatchers.html
Chapter: kotlin
Variables: Job job
Context: Coroutine

Name: The parent of job
Definition: the coroutine that started job
Page: https://kotlinlang.org/docs/coroutine-context-and-dispatchers.html
Chapter: kotlin
Variables: Job job
Context: Coroutine

Instruction: launch(
  job,
  fn
)
Page: https://kotlinlang.org/docs/coroutine-context-and-dispatchers.html
Effect: launch job fn, as a child of job
Language: kotlin
Context: Coroutine
Variables: Job job

Instruction: CoroutineName(name)
Page: https://kotlinlang.org/docs/coroutine-context-and-dispatchers.html
Language: kotlin
Context: Coroutine
Returns: A new name object for coroutine, that can be added in a context for debugging

Typ: AbstractCoroutineContextElement
Meaning: Base class for CoroutineContext.Element implementations.
Page: https://kotlinlang.org/docs/coroutine-context-and-dispatchers.html
Language: kotlin
Context: Coroutine

Instruction: context + contextElement
Page: https://kotlinlang.org/docs/coroutine-context-and-dispatchers.html
Language: kotlin
Context: Coroutine
Returns: the context, similar to context with also the contextElement
Variables: AbstractCoroutineContextElement contextElement
AbstractCoroutineContext context
Typ: AbstractCoroutineContextElement

Typ: MainScope
Meaning: A coroutine scope for UI application
Page: https://kotlinlang.org/docs/coroutine-context-and-dispatchers.html
Language: kotlin
Context: Coroutine

Typ: CoroutineScope
Meaning: A general purpose scope for coroutine
Page: https://kotlinlang.org/docs/coroutine-context-and-dispatchers.html
Language: kotlin
Context: Coroutine

Typ: ThreadLocal<T>
Meaning: A variable of type T whose value may differ depending on thread
Page: https://kotlinlang.org/docs/coroutine-context-and-dispatchers.html
Language: kotlin
Context: Coroutine

Instruction: threadLocal.set(value)
Page: https://kotlinlang.org/docs/coroutine-context-and-dispatchers.html
Effect: Set threadLocal's value in this thread to value
Language: kotlin
Context: Coroutine
Variables: ThreadLocal<T> threadLocal

Instruction: threadLocal.asContext(value = value)
Page: https://kotlinlang.org/docs/coroutine-context-and-dispatchers.html
Effect: A context for coroutine, where threadLocal will have value
Language: kotlin
Context: Coroutine
Variables: ThreadLocal<T> threadLocal

Name: A hot stream
Definition: A stream with all values already computed
Page: https://kotlinlang.org/docs/coroutine-context-and-dispatchers.html
Context: Kotlin
Example: listOf, mutableListOf

Name: Context preservation
Definition: A flow encapsulates its own execution context and never propagates or leaks it downstream
Page: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html
Context: Kotlin flow

Name: Exception transparency
Definition: When emit or emitAll throws, the Flow implementations must immediately stop emitting new values and finish with an exception.
Page: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html
Context: Kotlin flow

Typ: FlowCollector<in T>
Meaning: an entity that accepts values emitted by the Flow<out T>
Page: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html
Language: Kotlin
Context: flow

Typ: AtomicInteger
Meaning: An integer on which operations can be done atomically
Page: https://kotlinlang.org/docs/shared-mutable-state-and-concurrency.html#thread-safe-data-structures
Language: Kotlin

Variable: atomicInteger.incrementAndGet()
Meaning: increment the integer and return the result atomically
Page: https://kotlinlang.org/docs/shared-mutable-state-and-concurrency.html#thread-safe-data-structures
Language: Kotlin
Variables: AtomicInteger atomicInteger

Name: Thread confinement
Definition: an approach to the problem of shared mutable state where all access to the particular shared state is confined to a single thread
Page: https://kotlinlang.org/docs/shared-mutable-state-and-concurrency.html#thread-safe-data-structures
Context: Kotlin

Instruction: withContext(
  context,
  fn
)
Page: https://kotlinlang.org/docs/shared-mutable-state-and-concurrency.html#thread-safe-data-structures
Effect: Execute fn in context
Language: Kotlin

Instruction: Actor
Page: https://kotlinlang.org/docs/shared-mutable-state-and-concurrency.html#thread-safe-data-structures
Effect: todo
Language: Kotlin
Variables: Mutex mutex

Instruction: select
Page: https://kotlinlang.org/docs/select-expression.html
Effect: todo when not experimental
Language: Kotlin

Name: A main-safe function
Definition: A function that don't block UI update when run on the main thread
Page: https://developer.android.com/kotlin/coroutines?hl=fr&gclid=CjwKCAjw9e6SBhB2EiwA5myr9rRf-xx8iox39rMSlVfBIKZfS3cc-UIPtbaxz5lQA1hipc3bi5crDBoC8I0QAvD_BwE&gclsrc=aw.ds
Context: Kotlin android

Constant: Dispatchers.IO
Meaning: A context for coroutine that must access IO
Page: https://developer.android.com/kotlin/coroutines?hl=fr&gclid=CjwKCAjw9e6SBhB2EiwA5myr9rRf-xx8iox39rMSlVfBIKZfS3cc-UIPtbaxz5lQA1hipc3bi5crDBoC8I0QAvD_BwE&gclsrc=aw.ds
Language: Kotlin
Context: android