log("START")
launch {log("LAUNCH - START")
async {log("ASYNC - A - START")for(i in1..5){log("A - $i")delay(1000)}log("ASYNC - A - END")}
async {log("ASYNC - B - START")for(i in1..5){log("B - $i")delay(1000)}log("ASYNC - B - END")}log("LAUNCH - END")}log("END")
I/System.out:[main] START
I/System.out:[main] END
I/System.out:[ForkJoinPool.commonPool-worker-1] LAUNCH - START
I/System.out:[ForkJoinPool.commonPool-worker-1] LAUNCH - END
I/System.out:[ForkJoinPool.commonPool-worker-2] ASYNC - A - START
I/System.out:[ForkJoinPool.commonPool-worker-2] A -1
I/System.out:[ForkJoinPool.commonPool-worker-1] ASYNC - B - START
I/System.out:[ForkJoinPool.commonPool-worker-1] B -1
I/System.out:[ForkJoinPool.commonPool-worker-2] A -2
I/System.out:[ForkJoinPool.commonPool-worker-1] B -2
I/System.out:[ForkJoinPool.commonPool-worker-1] A -3
I/System.out:[ForkJoinPool.commonPool-worker-1] B -3
I/System.out:[ForkJoinPool.commonPool-worker-2] A -4
I/System.out:[ForkJoinPool.commonPool-worker-2] B -4
I/System.out:[ForkJoinPool.commonPool-worker-2] A -5
I/System.out:[ForkJoinPool.commonPool-worker-1] B -5
I/System.out:[ForkJoinPool.commonPool-worker-2] ASYNC - A - END
I/System.out:[ForkJoinPool.commonPool-worker-2] ASYNC - B - END
直列で非同期処理。
log("START")
launch {log("LAUNCH - START")
async {log("ASYNC - A - START")for(i in1..5){log("A - $i")delay(1000)}log("ASYNC - A - END")}.join()
async {log("ASYNC - B - START")for(i in1..5){log("B - $i")delay(1000)}log("ASYNC - B - END")}.join()log("LAUNCH - END")}log("END")
I/System.out:[main] START
I/System.out:[main] END
I/System.out:[ForkJoinPool.commonPool-worker-1] LAUNCH - START
I/System.out:[ForkJoinPool.commonPool-worker-2] ASYNC - A - START
I/System.out:[ForkJoinPool.commonPool-worker-2] A -1
I/System.out:[ForkJoinPool.commonPool-worker-2] A -2
I/System.out:[ForkJoinPool.commonPool-worker-2] A -3
I/System.out:[ForkJoinPool.commonPool-worker-2] A -4
I/System.out:[ForkJoinPool.commonPool-worker-2] A -5
I/System.out:[ForkJoinPool.commonPool-worker-2] ASYNC - A - END
I/System.out:[ForkJoinPool.commonPool-worker-1] ASYNC - B - START
I/System.out:[ForkJoinPool.commonPool-worker-1] B -1
I/System.out:[ForkJoinPool.commonPool-worker-1] B -2
I/System.out:[ForkJoinPool.commonPool-worker-1] B -3
I/System.out:[ForkJoinPool.commonPool-worker-1] B -4
I/System.out:[ForkJoinPool.commonPool-worker-1] B -5
I/System.out:[ForkJoinPool.commonPool-worker-1] ASYNC - B - END
I/System.out:[ForkJoinPool.commonPool-worker-3] LAUNCH - END
log("START")val job = launch {log("LAUNCH - START")async(coroutineContext){log("ASYNC - A - START")for(i in1..5){log("A - $i")delay(1000)}log("ASYNC - A - END")}.join()async(coroutineContext){log("ASYNC - B - START")for(i in1..5){log("B - $i")delay(1000)}log("ASYNC - B - END")}.join()log("LAUNCH - END")}
fab.setOnClickListener{
job.cancel()log("Canceled!! $job")}log("END")
I/System.out:[main] START
I/System.out:[main] END
I/System.out:[ForkJoinPool.commonPool-worker-1] LAUNCH - START
I/System.out:[ForkJoinPool.commonPool-worker-2] ASYNC - A - START
I/System.out:[ForkJoinPool.commonPool-worker-2] A -1
I/System.out:[ForkJoinPool.commonPool-worker-2] A -2
I/System.out:[ForkJoinPool.commonPool-worker-2] A -3
I/System.out:[ForkJoinPool.commonPool-worker-2] A -4
I/System.out:[ForkJoinPool.commonPool-worker-2] A -5
I/System.out:[ForkJoinPool.commonPool-worker-2] ASYNC - A - END
I/System.out:[ForkJoinPool.commonPool-worker-1] ASYNC - B - START
I/System.out:[ForkJoinPool.commonPool-worker-1] B -1
I/System.out:[ForkJoinPool.commonPool-worker-1] B -2
I/System.out:[main] Canceled!! StandaloneCoroutine{ Cancelled }@d4f0b6b
I/System.out:[main] START
I/System.out:[ForkJoinPool.commonPool-worker-1] LAUNCH - START
I/System.out:[main] END
I/System.out:[ForkJoinPool.commonPool-worker-2] ASYNC - A - START
I/System.out:[ForkJoinPool.commonPool-worker-2] A -1
I/System.out:[ForkJoinPool.commonPool-worker-2] A -2
I/System.out:[ForkJoinPool.commonPool-worker-2] A -3
I/System.out:[ForkJoinPool.commonPool-worker-2] A -4
I/System.out:[ForkJoinPool.commonPool-worker-2] A -5
I/System.out:[ForkJoinPool.commonPool-worker-2] ASYNC - A - END
I/System.out:[ForkJoinPool.commonPool-worker-1] A :12345
I/System.out:[ForkJoinPool.commonPool-worker-2] ASYNC - B - START
I/System.out:[ForkJoinPool.commonPool-worker-2] B -1
I/System.out:[ForkJoinPool.commonPool-worker-2] B -2
I/System.out:[main] Canceled!! StandaloneCoroutine{ Cancelled }@5dc2361
I/System.out:[main] START
I/System.out:[main] END
I/System.out:[main] LAUNCH - START
I/System.out:[main] ASYNC - A - START
I/System.out:[main] A -1
I/System.out:[main] A -2
I/System.out:[main] A -3
I/System.out:[main] A -4
I/System.out:[main] A -5
I/System.out:[main] ASYNC - A - END
I/System.out:[main] A :12345
I/System.out:[main] ASYNC - B - START
I/System.out:[main] B -1
I/System.out:[main] B -2
I/System.out:[main] B -3
I/System.out:[main] Canceled!! StandaloneCoroutine{Cancelling}@20ac62d
I/System.out:[main] START
I/System.out:[main] END
I/System.out:[main] LAUNCH - START
I/System.out:[ForkJoinPool.commonPool-worker-1] ASYNC - A - START
I/System.out:[ForkJoinPool.commonPool-worker-1] A -1
I/System.out:[ForkJoinPool.commonPool-worker-1] A -2
I/System.out:[ForkJoinPool.commonPool-worker-1] A -3
I/System.out:[ForkJoinPool.commonPool-worker-1] A -4
I/System.out:[ForkJoinPool.commonPool-worker-1] A -5
I/System.out:[ForkJoinPool.commonPool-worker-1] ASYNC - A - END
I/System.out:[main] A :12345
I/System.out:[ForkJoinPool.commonPool-worker-1] ASYNC - B - START
I/System.out:[ForkJoinPool.commonPool-worker-1] B -1
I/System.out:[ForkJoinPool.commonPool-worker-1] B -2
I/System.out:[ForkJoinPool.commonPool-worker-1] B -3
I/System.out:[main] Canceled!! StandaloneCoroutine{Cancelling}@d8e310c