$ whois private lateinit var aakira: User data class User(val name: String, val twitterId: String, val githubId: String, val company: String) println("Name : ${aakira.name}") println("Twitter Id : ${aakira.twitterId}") println("Github Id : ${aakira.githubId}") println("Company : ${aakira.company}")
@_a_akira AAkira CyberAgent, Inc. Akira Aratani private lateinit var aakira : User data class User(val name: String, val twitterId: String, val githubId: String, val company: String) print("Name : ${aakira.name}”) println("Github Id : ${aakira.githubId}") print("Twitter Id : ${aakira.twitterId}") println("Company : ${aakira.company}") $ whois
@_a_akira AAkira CyberAgent, Inc. Akira Aratani private lateinit var aakira : User data class User(val name: String, val twitterId: String, val githubId: String, val company: String) print("Name : ${aakira.name}”) println("Github Id : ${aakira.githubId}") print("Twitter Id : ${aakira.twitterId}") println("Company : ${aakira.company}") $ whois ,PUMJOॿಡຊॻ͍ͨIUUQTHPPHM"$K')
Coroutine builders Name Result Scope Description launch Job CoroutineScope Launches coroutine that does not have any result async Deferred CoroutineScope Returns a single value with the future result produce ProducerJob ProducerScope Produces a stream of elements actor ActorJob ActorScope Processes a stream of messages runBlocking T CoroutineScope Blocks the thread while the coroutine runs IUUQTHJUIVCDPN,PUMJOLPUMJOYDPSPVUJOFT
launchとasyncの違い • launchはJobを返す • asyncはDeferredを返す • DeferredはJobのinterface public interface Deferred : Job { val isCompletedExceptionally: Boolean val isCancelled: Boolean public suspend fun await(): T public fun registerSelectAwait(select: SelectInstance, block: suspend (T) -> R) public fun getCompleted(): T }
Coroutine dispatchers Name Description CommonPool Confines coroutine execution to a shared pool of threads newSingleThreadContext Create new single-threaded coroutine context newFixedThreadPoolContext Creates new thread pool of a fixed size Executor.asCoroutineDispatcher Extension to convert any executor Unconfined Does not confine coroutine execution in any way IUUQTHJUIVCDPN,PUMJOLPUMJOYDPSPVUJOFT
Example 3 @Android // for core compile “org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinCoroutinesVersion” // for JDK8 compile “org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:$kotlinCoroutinesVersion” // for Asynchronous IO on JDK7+ compile “org.jetbrains.kotlinx:kotlinx-coroutines-nio:$kotlinCoroutinesVersion" // for RxJava2 compile “org.jetbrains.kotlinx:kotlinx-coroutines-rx2:$kotlinCoroutinesVersion" // for Android compile “org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlinCoroutinesVersion" // for Swing compile “org.jetbrains.kotlinx:kotlinx-coroutines-swing:$kotlinCoroutinesVersion" // for JavaFX compile “org.jetbrains.kotlinx:kotlinx-coroutines-javafx:$kotlinCoroutinesVersion" "OESPJE༻ʹ$PSPVUJOFEJTQBUDIFS͕༻ҙ͞Ε͍ͯΔ
Bytecode • 2つの中断があるコルーチン • 状態 A. 1つ目の中断の前 B. 1つ目の中断後 C. 2つ目の中断後 val a = a() val y = foo(a).await() // suspension point #1 b() val z = bar(a, y).await() // suspension point #2 c(z)
class extends CoroutineImpl<...> implements Continuation { // The current state of the state machine int label = 0 // local variables of the coroutine A a = null Y y = null void resume(Object data) { if (label == 0) goto L0 if (label == 1) goto L1 if (label == 2) goto L2 else throw IllegalStateException() L0: // data is expected to be `null` at this invocation a = a() label = 1 data = foo(a).await(this) // 'this' is passed as a continuation if (data == COROUTINE_SUSPENDED) return // return if await had suspended execution L1: // external code has resumed this coroutine passing the result of .await() as data y = (Y) data b() label = 2 data = bar(a, y).await(this) // 'this' is passed as a continuation if (data == COROUTINE_SUSPENDED) return // return if await had suspended execution L2: // external code has resumed this coroutine passing the result of .await() as data Z z = (Z) data c(z) label = -1 // No more steps are allowed return } }
class extends CoroutineImpl<...> implements Continuation { // The current state of the state machine int label = 0 // local variables of the coroutine A a = null Y y = null void resume(Object data) { if (label == 0) goto L0 if (label == 1) goto L1 if (label == 2) goto L2 else throw IllegalStateException() L0: // data is expected to be `null` at this invocation a = a() label = 1 data = foo(a).await(this) // 'this' is passed as a continuation if (data == COROUTINE_SUSPENDED) return // return if await had suspended execution L1: // external code has resumed this coroutine passing the result of .await() as data y = (Y) data b() label = 2 data = bar(a, y).await(this) // 'this' is passed as a continuation if (data == COROUTINE_SUSPENDED) return // return if await had suspended execution L2: // external code has resumed this coroutine passing the result of .await() as data Z z = (Z) data c(z) label = -1 // No more steps are allowed return } }
class extends CoroutineImpl<...> implements Continuation { // The current state of the state machine int label = 0 // local variables of the coroutine A a = null Y y = null void resume(Object data) { if (label == 0) goto L0 if (label == 1) goto L1 if (label == 2) goto L2 else throw IllegalStateException() L0: // data is expected to be `null` at this invocation a = a() label = 1 data = foo(a).await(this) // 'this' is passed as a continuation if (data == COROUTINE_SUSPENDED) return // return if await had suspended execution L1: // external code has resumed this coroutine passing the result of .await() as data y = (Y) data b() label = 2 data = bar(a, y).await(this) // 'this' is passed as a continuation if (data == COROUTINE_SUSPENDED) return // return if await had suspended execution L2: // external code has resumed this coroutine passing the result of .await() as data Z z = (Z) data c(z) label = -1 // No more steps are allowed return } }
class extends CoroutineImpl<...> implements Continuation { // The current state of the state machine int label = 0 // local variables of the coroutine A a = null Y y = null void resume(Object data) { if (label == 0) goto L0 if (label == 1) goto L1 if (label == 2) goto L2 else throw IllegalStateException() L0: // data is expected to be `null` at this invocation a = a() label = 1 data = foo(a).await(this) // 'this' is passed as a continuation if (data == COROUTINE_SUSPENDED) return // return if await had suspended execution L1: // external code has resumed this coroutine passing the result of .await() as data y = (Y) data b() label = 2 data = bar(a, y).await(this) // 'this' is passed as a continuation if (data == COROUTINE_SUSPENDED) return // return if await had suspended execution L2: // external code has resumed this coroutine passing the result of .await() as data Z z = (Z) data c(z) label = -1 // No more steps are allowed return } }