of program instructions that performs a speci fi c task, packaged as a unit • In di ff erent programming languages, a subroutine may be called a procedure, a function, a routine, a method, or a subprogram. The generic term callable unit is sometimes used.
term coroutine in 1958 when he applied it to construction of an assembly program. • The fi rst published explanation of the coroutine appeared later, in 1963
non-preemptive multitasking, by allowing multiple entry points for suspending and resuming execution at certain locations. • Coroutines are well-suited for implementing familiar program components such as cooperative tasks, exceptions, event loops, iterators, in fi nite lists and pipes. : https://en.wikipedia.org/wiki/Coroutine
part of the development landscape. When creating server-side, desktop, or mobile applications, it's important to provide an experience that is not only fl uid from the user's perspective, but also scalable when needed. • Kotlin solves this problem in a fl exible way by providing coroutine support at the language level and delegating most of the functionality to libraries. • In addition to opening the doors to asynchronous programming, coroutines also provide a wealth of other possibilities, such as concurrency and actors.
this scope. * Context is encapsulated by the scope and used for implementation of coroutine builders that are extensions on the scope. * Accessing this property in general code is not recommended for any purposes except accessing the [Job] instance for advanced usages. * * By convention, should contain an instance of a [job][Job] to enforce structured concurrency. */ public val coroutineContext: CoroutineContext } CoroutineScope ୭࢚ਤ interface ੑפ.
CoroutineContext = if (context === EmptyCoroutineContext) this else // fast path -- avoid lambda creation context.fold(this) { acc, element -> val removed = acc.minusKey(element.key) if (removed === EmptyCoroutineContext) element else { // make sure interceptor is always last in the context (and thus is fast to get when present) val interceptor = removed[ContinuationInterceptor] if (interceptor == null) CombinedContext(removed, element) else { val left = removed.minusKey(ContinuationInterceptor) if (left === EmptyCoroutineContext) CombinedContext(element, interceptor) else CombinedContext(CombinedContext(left, element), interceptor) } } } } CoroutineContext ղࠗ ٘ plus ࠗ࠙ੑפ.
context of the coroutine that corresponds to this continuation. */ public val context: CoroutineContext /** * Resumes the execution of the corresponding coroutine passing a successful or failed [result] as the * return value of the last suspension point. */ public fun resumeWith(result: Result<T>) } suspend ٣ஹੌ द ֢ػ Continuation<T> interfaceח resumeWith()ਸ ನೣפ. Continuationীࢲ ਵ۽ ࠂӈೡ ٸח resumeWith()ਸ ഐ೧ чਸ ֈѹݶ ࢚క۽ جইцפ.
/* * Sent successfully -- bail out. * But failure may indicate either that the channel it full or that * it is close. Go to slow path on failure to simplify the successful path and * to materialize default exception. */ trySend(element).onSuccess { return ChannelResult.success(Unit) } return runBlocking { val r = runCatching { send(element) } if (r.isSuccess) ChannelResult.success(Unit) else ChannelResult.closed(r.exceptionOrNull()) } } trySendBlocking ղࠗ ٘ীࢲ ঌ ࣻ ݅ runBlockingਸ ഝਊೞҊ णפ. runBlocking UIী ೠ lock ߊࢤೡ ࣻ ӝ ٸޙ ী trySendBlocking ٘ ࢎਊ द UI৬ ޖҙೠ Ҕীࢲ ࢎਊ ਃೞפ ೧ࢲ ࢎਊ೧ঠ פ.