"+"

2

これはKotlinコルーチンのためCancellation via explicit jobのためのサンプルコードです:"+"

fun main(args: Array<String>) = runBlocking<Unit> { 
    val job = Job() // create a job object to manage our lifecycle 

    // now launch ten coroutines for a demo, each working for a different time 
    val coroutines = List(10) { i -> 
     // they are all children of our job object 
     launch(coroutineContext + job) { // we use the context of main runBlocking thread, but with our own job object 
      delay((i + 1) * 200L) // variable delay 200ms, 400ms, ... etc 
      println("Coroutine $i is done") 
     } 
    } 
    println("Launched ${coroutines.size} coroutines") 
    delay(500L) // delay for half a second 
    println("Cancelling the job!") 
    job.cancelAndJoin() // cancel all our coroutines and wait for all of them to complete 
} 

私は表現coroutineContext + job+について混乱していますか?

何がしているのですか?演算子は上書きされますか?

+0

これは同じドキュメントに説明されています:https://github.com/Kotlin/kotlinx.coroutines/blob/master/coroutines-guide.md#combining-contexts –

答えて

3

operator overloadingの例です。 以下に方法CoroutineContext::plusのドキュメントを示す:

open operator fun plus(context: CoroutineContext): CoroutineContext 

は、他のコンテキストからこのコンテキストの要素と要素を含むコンテキストを返します。他のものと同じキーを持つこのコンテキストの要素は削除されます。

基本的に2つのコンテキストのマージです。

+2

ここでは、いつでもそれに対応する方法は、IDEAのあなたのコードの '+'シンボルのCtrl +クリック(Macの場合はCmd + Click)でドキュメントを見る方法です。 –