Make examples runnable
* Also drop args from fun main
diff --git a/core/kotlinx-coroutines-core/test/guide/example-basic-01.kt b/core/kotlinx-coroutines-core/test/guide/example-basic-01.kt
index 3cad864..d83644f 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-basic-01.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-basic-01.kt
@@ -7,7 +7,7 @@
import kotlinx.coroutines.*
-fun main(args: Array<String>) {
+fun main() {
GlobalScope.launch { // launch new coroutine in background and continue
delay(1000L) // non-blocking delay for 1 second (default time unit is ms)
println("World!") // print after delay
diff --git a/core/kotlinx-coroutines-core/test/guide/example-basic-02.kt b/core/kotlinx-coroutines-core/test/guide/example-basic-02.kt
index c85e254..a47f906 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-basic-02.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-basic-02.kt
@@ -7,7 +7,7 @@
import kotlinx.coroutines.*
-fun main(args: Array<String>) {
+fun main() {
GlobalScope.launch { // launch new coroutine in background and continue
delay(1000L)
println("World!")
diff --git a/core/kotlinx-coroutines-core/test/guide/example-basic-02b.kt b/core/kotlinx-coroutines-core/test/guide/example-basic-02b.kt
index d65ddf9..8f179fa 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-basic-02b.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-basic-02b.kt
@@ -7,7 +7,7 @@
import kotlinx.coroutines.*
-fun main(args: Array<String>) = runBlocking<Unit> { // start main coroutine
+fun main() = runBlocking<Unit> { // start main coroutine
GlobalScope.launch { // launch new coroutine in background and continue
delay(1000L)
println("World!")
diff --git a/core/kotlinx-coroutines-core/test/guide/example-basic-03.kt b/core/kotlinx-coroutines-core/test/guide/example-basic-03.kt
index f153890..861a602 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-basic-03.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-basic-03.kt
@@ -7,11 +7,13 @@
import kotlinx.coroutines.*
-fun main(args: Array<String>) = runBlocking {
+fun main() = runBlocking {
+//sampleStart
val job = GlobalScope.launch { // launch new coroutine and keep a reference to its Job
delay(1000L)
println("World!")
}
println("Hello,")
job.join() // wait until child coroutine completes
+//sampleEnd
}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-basic-03s.kt b/core/kotlinx-coroutines-core/test/guide/example-basic-03s.kt
index b9b6aa2..1e1800d 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-basic-03s.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-basic-03s.kt
@@ -7,7 +7,7 @@
import kotlinx.coroutines.*
-fun main(args: Array<String>) = runBlocking { // this: CoroutineScope
+fun main() = runBlocking { // this: CoroutineScope
launch { // launch new coroutine in the scope of runBlocking
delay(1000L)
println("World!")
diff --git a/core/kotlinx-coroutines-core/test/guide/example-basic-04.kt b/core/kotlinx-coroutines-core/test/guide/example-basic-04.kt
index 76ca19b..b0e2f73 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-basic-04.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-basic-04.kt
@@ -7,7 +7,7 @@
import kotlinx.coroutines.*
-fun main(args: Array<String>) = runBlocking { // this: CoroutineScope
+fun main() = runBlocking { // this: CoroutineScope
launch {
delay(200L)
println("Task from runBlocking")
diff --git a/core/kotlinx-coroutines-core/test/guide/example-basic-05.kt b/core/kotlinx-coroutines-core/test/guide/example-basic-05.kt
index 06067f1..7a4340e 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-basic-05.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-basic-05.kt
@@ -7,7 +7,7 @@
import kotlinx.coroutines.*
-fun main(args: Array<String>) = runBlocking {
+fun main() = runBlocking {
launch { doWorld() }
println("Hello,")
}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-basic-06.kt b/core/kotlinx-coroutines-core/test/guide/example-basic-06.kt
index 2f351b4..ff6db92 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-basic-06.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-basic-06.kt
@@ -7,7 +7,7 @@
import kotlinx.coroutines.*
-fun main(args: Array<String>) = runBlocking {
+fun main() = runBlocking {
repeat(100_000) { // launch a lot of coroutines
launch {
delay(1000L)
diff --git a/core/kotlinx-coroutines-core/test/guide/example-basic-07.kt b/core/kotlinx-coroutines-core/test/guide/example-basic-07.kt
index 8df75bc..58eadd6 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-basic-07.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-basic-07.kt
@@ -7,7 +7,8 @@
import kotlinx.coroutines.*
-fun main(args: Array<String>) = runBlocking {
+fun main() = runBlocking {
+//sampleStart
GlobalScope.launch {
repeat(1000) { i ->
println("I'm sleeping $i ...")
@@ -15,4 +16,5 @@
}
}
delay(1300L) // just quit after delay
+//sampleEnd
}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-cancel-01.kt b/core/kotlinx-coroutines-core/test/guide/example-cancel-01.kt
index 9288873..c2a7b0b 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-cancel-01.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-cancel-01.kt
@@ -7,7 +7,8 @@
import kotlinx.coroutines.*
-fun main(args: Array<String>) = runBlocking {
+fun main() = runBlocking {
+//sampleStart
val job = launch {
repeat(1000) { i ->
println("I'm sleeping $i ...")
@@ -19,4 +20,5 @@
job.cancel() // cancels the job
job.join() // waits for job's completion
println("main: Now I can quit.")
+//sampleEnd
}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-cancel-02.kt b/core/kotlinx-coroutines-core/test/guide/example-cancel-02.kt
index 1d3c82d..3618a3b 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-cancel-02.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-cancel-02.kt
@@ -7,7 +7,8 @@
import kotlinx.coroutines.*
-fun main(args: Array<String>) = runBlocking {
+fun main() = runBlocking {
+//sampleStart
val startTime = timeSource.currentTimeMillis()
val job = launch(Dispatchers.Default) {
var nextPrintTime = startTime
@@ -24,4 +25,5 @@
println("main: I'm tired of waiting!")
job.cancelAndJoin() // cancels the job and waits for its completion
println("main: Now I can quit.")
+//sampleEnd
}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-cancel-03.kt b/core/kotlinx-coroutines-core/test/guide/example-cancel-03.kt
index 3d98efc..edd9ef0 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-cancel-03.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-cancel-03.kt
@@ -7,7 +7,8 @@
import kotlinx.coroutines.*
-fun main(args: Array<String>) = runBlocking {
+fun main() = runBlocking {
+//sampleStart
val startTime = timeSource.currentTimeMillis()
val job = launch(Dispatchers.Default) {
var nextPrintTime = startTime
@@ -24,4 +25,5 @@
println("main: I'm tired of waiting!")
job.cancelAndJoin() // cancels the job and waits for its completion
println("main: Now I can quit.")
+//sampleEnd
}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-cancel-04.kt b/core/kotlinx-coroutines-core/test/guide/example-cancel-04.kt
index 2001e41..4d530f7 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-cancel-04.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-cancel-04.kt
@@ -7,7 +7,8 @@
import kotlinx.coroutines.*
-fun main(args: Array<String>) = runBlocking {
+fun main() = runBlocking {
+//sampleStart
val job = launch {
try {
repeat(1000) { i ->
@@ -22,4 +23,5 @@
println("main: I'm tired of waiting!")
job.cancelAndJoin() // cancels the job and waits for its completion
println("main: Now I can quit.")
+//sampleEnd
}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-cancel-05.kt b/core/kotlinx-coroutines-core/test/guide/example-cancel-05.kt
index 530fff1..d977d4e 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-cancel-05.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-cancel-05.kt
@@ -7,7 +7,8 @@
import kotlinx.coroutines.*
-fun main(args: Array<String>) = runBlocking {
+fun main() = runBlocking {
+//sampleStart
val job = launch {
try {
repeat(1000) { i ->
@@ -26,4 +27,5 @@
println("main: I'm tired of waiting!")
job.cancelAndJoin() // cancels the job and waits for its completion
println("main: Now I can quit.")
+//sampleEnd
}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-cancel-06.kt b/core/kotlinx-coroutines-core/test/guide/example-cancel-06.kt
index 84f3038..da8ecbd 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-cancel-06.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-cancel-06.kt
@@ -7,11 +7,13 @@
import kotlinx.coroutines.*
-fun main(args: Array<String>) = runBlocking {
+fun main() = runBlocking {
+//sampleStart
withTimeout(1300L) {
repeat(1000) { i ->
println("I'm sleeping $i ...")
delay(500L)
}
}
+//sampleEnd
}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-cancel-07.kt b/core/kotlinx-coroutines-core/test/guide/example-cancel-07.kt
index 4596c30..3fbbd09 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-cancel-07.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-cancel-07.kt
@@ -7,7 +7,8 @@
import kotlinx.coroutines.*
-fun main(args: Array<String>) = runBlocking {
+fun main() = runBlocking {
+//sampleStart
val result = withTimeoutOrNull(1300L) {
repeat(1000) { i ->
println("I'm sleeping $i ...")
@@ -16,4 +17,5 @@
"Done" // will get cancelled before it produces this result
}
println("Result is $result")
+//sampleEnd
}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-channel-01.kt b/core/kotlinx-coroutines-core/test/guide/example-channel-01.kt
index 79013bb..ab437ed 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-channel-01.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-channel-01.kt
@@ -8,7 +8,8 @@
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
-fun main(args: Array<String>) = runBlocking {
+fun main() = runBlocking {
+//sampleStart
val channel = Channel<Int>()
launch {
// this might be heavy CPU-consuming computation or async logic, we'll just send five squares
@@ -17,4 +18,5 @@
// here we print five received integers:
repeat(5) { println(channel.receive()) }
println("Done!")
+//sampleEnd
}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-channel-02.kt b/core/kotlinx-coroutines-core/test/guide/example-channel-02.kt
index 3489545..d65f0f2 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-channel-02.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-channel-02.kt
@@ -8,7 +8,8 @@
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
-fun main(args: Array<String>) = runBlocking {
+fun main() = runBlocking {
+//sampleStart
val channel = Channel<Int>()
launch {
for (x in 1..5) channel.send(x * x)
@@ -17,4 +18,5 @@
// here we print received values using `for` loop (until the channel is closed)
for (y in channel) println(y)
println("Done!")
+//sampleEnd
}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-channel-03.kt b/core/kotlinx-coroutines-core/test/guide/example-channel-03.kt
index 4bce963..84419aa 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-channel-03.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-channel-03.kt
@@ -12,8 +12,10 @@
for (x in 1..5) send(x * x)
}
-fun main(args: Array<String>) = runBlocking {
+fun main() = runBlocking {
+//sampleStart
val squares = produceSquares()
squares.consumeEach { println(it) }
println("Done!")
+//sampleEnd
}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-channel-04.kt b/core/kotlinx-coroutines-core/test/guide/example-channel-04.kt
index f4a4f83..eefbf2a 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-channel-04.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-channel-04.kt
@@ -8,19 +8,24 @@
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
-fun CoroutineScope.produceNumbers() = produce<Int> {
- var x = 1
- while (true) send(x++) // infinite stream of integers starting from 1
-}
-
-fun CoroutineScope.square(numbers: ReceiveChannel<Int>): ReceiveChannel<Int> = produce {
- for (x in numbers) send(x * x)
-}
-
-fun main(args: Array<String>) = runBlocking {
+fun main() = runBlocking {
+//sampleStart
val numbers = produceNumbers() // produces integers from 1 and on
val squares = square(numbers) // squares integers
for (i in 1..5) println(squares.receive()) // print first five
println("Done!") // we are done
coroutineContext.cancelChildren() // cancel children coroutines
+//sampleEnd
+}
+
+fun CoroutineScope.produceSquares(): ReceiveChannel<Int> = produce {
+ for (x in 1..5) send(x * x)
+}
+
+fun CoroutineScope.produceNumbers() = produce<Int> {
+ var x = 1
+ while (true) send(x++) // infinite stream of integers starting from 1
+}
+fun CoroutineScope.square(numbers: ReceiveChannel<Int>): ReceiveChannel<Int> = produce {
+ for (x in numbers) send(x * x)
}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-channel-05.kt b/core/kotlinx-coroutines-core/test/guide/example-channel-05.kt
index ce1e999..5f9d247 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-channel-05.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-channel-05.kt
@@ -7,7 +7,18 @@
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
-import kotlin.coroutines.*
+
+fun main() = runBlocking {
+//sampleStart
+ var cur = numbersFrom(2)
+ for (i in 1..10) {
+ val prime = cur.receive()
+ println(prime)
+ cur = filter(cur, prime)
+ }
+ coroutineContext.cancelChildren() // cancel all children to let main finish
+//sampleEnd
+}
fun CoroutineScope.numbersFrom(start: Int) = produce<Int> {
var x = start
@@ -17,13 +28,3 @@
fun CoroutineScope.filter(numbers: ReceiveChannel<Int>, prime: Int) = produce<Int> {
for (x in numbers) if (x % prime != 0) send(x)
}
-
-fun main(args: Array<String>) = runBlocking {
- var cur = numbersFrom(2)
- for (i in 1..10) {
- val prime = cur.receive()
- println(prime)
- cur = filter(cur, prime)
- }
- coroutineContext.cancelChildren() // cancel all children to let main finish
-}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-channel-06.kt b/core/kotlinx-coroutines-core/test/guide/example-channel-06.kt
index c9837d0..7224ffa 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-channel-06.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-channel-06.kt
@@ -8,6 +8,15 @@
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
+fun main() = runBlocking<Unit> {
+//sampleStart
+ val producer = produceNumbers()
+ repeat(5) { launchProcessor(it, producer) }
+ delay(950)
+ producer.cancel() // cancel producer coroutine and thus kill them all
+//sampleEnd
+}
+
fun CoroutineScope.produceNumbers() = produce<Int> {
var x = 1 // start from 1
while (true) {
@@ -21,10 +30,3 @@
println("Processor #$id received $msg")
}
}
-
-fun main(args: Array<String>) = runBlocking<Unit> {
- val producer = produceNumbers()
- repeat(5) { launchProcessor(it, producer) }
- delay(950)
- producer.cancel() // cancel producer coroutine and thus kill them all
-}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-channel-07.kt b/core/kotlinx-coroutines-core/test/guide/example-channel-07.kt
index 6f84775..64df2ec 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-channel-07.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-channel-07.kt
@@ -7,16 +7,9 @@
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
-import kotlin.coroutines.*
-suspend fun sendString(channel: SendChannel<String>, s: String, time: Long) {
- while (true) {
- delay(time)
- channel.send(s)
- }
-}
-
-fun main(args: Array<String>) = runBlocking {
+fun main() = runBlocking {
+//sampleStart
val channel = Channel<String>()
launch { sendString(channel, "foo", 200L) }
launch { sendString(channel, "BAR!", 500L) }
@@ -24,4 +17,12 @@
println(channel.receive())
}
coroutineContext.cancelChildren() // cancel all children to let main finish
+//sampleEnd
+}
+
+suspend fun sendString(channel: SendChannel<String>, s: String, time: Long) {
+ while (true) {
+ delay(time)
+ channel.send(s)
+ }
}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-channel-08.kt b/core/kotlinx-coroutines-core/test/guide/example-channel-08.kt
index 928d03b..f1e9d9b 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-channel-08.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-channel-08.kt
@@ -7,9 +7,9 @@
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
-import kotlin.coroutines.*
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
+//sampleStart
val channel = Channel<Int>(4) // create buffered channel
val sender = launch { // launch sender coroutine
repeat(10) {
@@ -20,4 +20,5 @@
// don't receive anything... just wait....
delay(1000)
sender.cancel() // cancel sender coroutine
+//sampleEnd
}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-channel-09.kt b/core/kotlinx-coroutines-core/test/guide/example-channel-09.kt
index c5d6dcd..ee7afab 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-channel-09.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-channel-09.kt
@@ -7,11 +7,11 @@
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
-import kotlin.coroutines.*
+//sampleStart
data class Ball(var hits: Int)
-fun main(args: Array<String>) = runBlocking {
+fun main() = runBlocking {
val table = Channel<Ball>() // a shared table
launch { player("ping", table) }
launch { player("pong", table) }
@@ -28,3 +28,4 @@
table.send(ball) // send the ball back
}
}
+//sampleEnd
diff --git a/core/kotlinx-coroutines-core/test/guide/example-channel-10.kt b/core/kotlinx-coroutines-core/test/guide/example-channel-10.kt
index 23d61cd..4615e30 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-channel-10.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-channel-10.kt
@@ -8,7 +8,7 @@
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
val tickerChannel = ticker(delayMillis = 100, initialDelayMillis = 0) // create ticker channel
var nextElement = withTimeoutOrNull(1) { tickerChannel.receive() }
println("Initial element is available immediately: $nextElement") // initial delay hasn't passed yet
diff --git a/core/kotlinx-coroutines-core/test/guide/example-compose-01.kt b/core/kotlinx-coroutines-core/test/guide/example-compose-01.kt
index e593b29..6c027e4 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-compose-01.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-compose-01.kt
@@ -8,6 +8,17 @@
import kotlinx.coroutines.*
import kotlin.system.*
+fun main() = runBlocking<Unit> {
+//sampleStart
+ val time = measureTimeMillis {
+ val one = doSomethingUsefulOne()
+ val two = doSomethingUsefulTwo()
+ println("The answer is ${one + two}")
+ }
+ println("Completed in $time ms")
+//sampleEnd
+}
+
suspend fun doSomethingUsefulOne(): Int {
delay(1000L) // pretend we are doing something useful here
return 13
@@ -17,12 +28,3 @@
delay(1000L) // pretend we are doing something useful here, too
return 29
}
-
-fun main(args: Array<String>) = runBlocking<Unit> {
- val time = measureTimeMillis {
- val one = doSomethingUsefulOne()
- val two = doSomethingUsefulTwo()
- println("The answer is ${one + two}")
- }
- println("Completed in $time ms")
-}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-compose-02.kt b/core/kotlinx-coroutines-core/test/guide/example-compose-02.kt
index 0890eef..1cb75d9 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-compose-02.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-compose-02.kt
@@ -8,6 +8,17 @@
import kotlinx.coroutines.*
import kotlin.system.*
+fun main() = runBlocking<Unit> {
+//sampleStart
+ val time = measureTimeMillis {
+ val one = async { doSomethingUsefulOne() }
+ val two = async { doSomethingUsefulTwo() }
+ println("The answer is ${one.await() + two.await()}")
+ }
+ println("Completed in $time ms")
+//sampleEnd
+}
+
suspend fun doSomethingUsefulOne(): Int {
delay(1000L) // pretend we are doing something useful here
return 13
@@ -17,12 +28,3 @@
delay(1000L) // pretend we are doing something useful here, too
return 29
}
-
-fun main(args: Array<String>) = runBlocking<Unit> {
- val time = measureTimeMillis {
- val one = async { doSomethingUsefulOne() }
- val two = async { doSomethingUsefulTwo() }
- println("The answer is ${one.await() + two.await()}")
- }
- println("Completed in $time ms")
-}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-compose-03.kt b/core/kotlinx-coroutines-core/test/guide/example-compose-03.kt
index 925922e..4a919fb 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-compose-03.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-compose-03.kt
@@ -8,17 +8,8 @@
import kotlinx.coroutines.*
import kotlin.system.*
-suspend fun doSomethingUsefulOne(): Int {
- delay(1000L) // pretend we are doing something useful here
- return 13
-}
-
-suspend fun doSomethingUsefulTwo(): Int {
- delay(1000L) // pretend we are doing something useful here, too
- return 29
-}
-
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
+//sampleStart
val time = measureTimeMillis {
val one = async(start = CoroutineStart.LAZY) { doSomethingUsefulOne() }
val two = async(start = CoroutineStart.LAZY) { doSomethingUsefulTwo() }
@@ -28,4 +19,15 @@
println("The answer is ${one.await() + two.await()}")
}
println("Completed in $time ms")
+//sampleEnd
+}
+
+suspend fun doSomethingUsefulOne(): Int {
+ delay(1000L) // pretend we are doing something useful here
+ return 13
+}
+
+suspend fun doSomethingUsefulTwo(): Int {
+ delay(1000L) // pretend we are doing something useful here, too
+ return 29
}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-compose-04.kt b/core/kotlinx-coroutines-core/test/guide/example-compose-04.kt
index af6e2fe..159d0fb 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-compose-04.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-compose-04.kt
@@ -8,28 +8,9 @@
import kotlinx.coroutines.*
import kotlin.system.*
-suspend fun doSomethingUsefulOne(): Int {
- delay(1000L) // pretend we are doing something useful here
- return 13
-}
-
-suspend fun doSomethingUsefulTwo(): Int {
- delay(1000L) // pretend we are doing something useful here, too
- return 29
-}
-
-// The result type of somethingUsefulOneAsync is Deferred<Int>
-fun somethingUsefulOneAsync() = GlobalScope.async {
- doSomethingUsefulOne()
-}
-
-// The result type of somethingUsefulTwoAsync is Deferred<Int>
-fun somethingUsefulTwoAsync() = GlobalScope.async {
- doSomethingUsefulTwo()
-}
-
+//sampleStart
// note, that we don't have `runBlocking` to the right of `main` in this example
-fun main(args: Array<String>) {
+fun main() {
val time = measureTimeMillis {
// we can initiate async actions outside of a coroutine
val one = somethingUsefulOneAsync()
@@ -42,3 +23,22 @@
}
println("Completed in $time ms")
}
+//sampleEnd
+
+fun somethingUsefulOneAsync() = GlobalScope.async {
+ doSomethingUsefulOne()
+}
+
+fun somethingUsefulTwoAsync() = GlobalScope.async {
+ doSomethingUsefulTwo()
+}
+
+suspend fun doSomethingUsefulOne(): Int {
+ delay(1000L) // pretend we are doing something useful here
+ return 13
+}
+
+suspend fun doSomethingUsefulTwo(): Int {
+ delay(1000L) // pretend we are doing something useful here, too
+ return 29
+}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-compose-05.kt b/core/kotlinx-coroutines-core/test/guide/example-compose-05.kt
index 4a244d4..07c0e97 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-compose-05.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-compose-05.kt
@@ -8,14 +8,13 @@
import kotlinx.coroutines.*
import kotlin.system.*
-suspend fun doSomethingUsefulOne(): Int {
- delay(1000L) // pretend we are doing something useful here
- return 13
-}
-
-suspend fun doSomethingUsefulTwo(): Int {
- delay(1000L) // pretend we are doing something useful here, too
- return 29
+fun main() = runBlocking<Unit> {
+//sampleStart
+ val time = measureTimeMillis {
+ println("The answer is ${concurrentSum()}")
+ }
+ println("Completed in $time ms")
+//sampleEnd
}
suspend fun concurrentSum(): Int = coroutineScope {
@@ -24,9 +23,12 @@
one.await() + two.await()
}
-fun main(args: Array<String>) = runBlocking<Unit> {
- val time = measureTimeMillis {
- println("The answer is ${concurrentSum()}")
- }
- println("Completed in $time ms")
+suspend fun doSomethingUsefulOne(): Int {
+ delay(1000L) // pretend we are doing something useful here
+ return 13
+}
+
+suspend fun doSomethingUsefulTwo(): Int {
+ delay(1000L) // pretend we are doing something useful here, too
+ return 29
}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-compose-06.kt b/core/kotlinx-coroutines-core/test/guide/example-compose-06.kt
index d128896..6fc92a2 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-compose-06.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-compose-06.kt
@@ -6,19 +6,8 @@
package kotlinx.coroutines.guide.compose06
import kotlinx.coroutines.*
-import kotlin.system.*
-suspend fun doSomethingUsefulOne(): Int {
- delay(1000L) // pretend we are doing something useful here
- return 13
-}
-
-suspend fun doSomethingUsefulTwo(): Int {
- delay(1000L) // pretend we are doing something useful here, too
- return 29
-}
-
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
try {
failedConcurrentSum()
} catch(e: ArithmeticException) {
diff --git a/core/kotlinx-coroutines-core/test/guide/example-context-01.kt b/core/kotlinx-coroutines-core/test/guide/example-context-01.kt
index 65f2ece..1471f72 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-context-01.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-context-01.kt
@@ -6,9 +6,9 @@
package kotlinx.coroutines.guide.context01
import kotlinx.coroutines.*
-import kotlin.coroutines.*
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
+//sampleStart
launch { // context of the parent, main runBlocking coroutine
println("main runBlocking : I'm working in thread ${Thread.currentThread().name}")
}
@@ -21,4 +21,5 @@
launch(newSingleThreadContext("MyOwnThread")) { // will get its own new thread
println("newSingleThreadContext: I'm working in thread ${Thread.currentThread().name}")
}
+//sampleEnd
}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-context-02.kt b/core/kotlinx-coroutines-core/test/guide/example-context-02.kt
index 038102d..7a29aed 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-context-02.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-context-02.kt
@@ -6,9 +6,9 @@
package kotlinx.coroutines.guide.context02
import kotlinx.coroutines.*
-import kotlin.coroutines.*
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
+//sampleStart
launch(Dispatchers.Unconfined) { // not confined -- will work with main thread
println("Unconfined : I'm working in thread ${Thread.currentThread().name}")
delay(500)
@@ -19,4 +19,5 @@
delay(1000)
println("main runBlocking: After delay in thread ${Thread.currentThread().name}")
}
+//sampleEnd
}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-context-03.kt b/core/kotlinx-coroutines-core/test/guide/example-context-03.kt
index 9e50724..5740495 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-context-03.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-context-03.kt
@@ -6,11 +6,11 @@
package kotlinx.coroutines.guide.context03
import kotlinx.coroutines.*
-import kotlin.coroutines.*
fun log(msg: String) = println("[${Thread.currentThread().name}] $msg")
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
+//sampleStart
val a = async {
log("I'm computing a piece of the answer")
6
@@ -20,4 +20,5 @@
7
}
log("The answer is ${a.await() * b.await()}")
+//sampleEnd
}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-context-04.kt b/core/kotlinx-coroutines-core/test/guide/example-context-04.kt
index 45f968b..6624252 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-context-04.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-context-04.kt
@@ -9,7 +9,8 @@
fun log(msg: String) = println("[${Thread.currentThread().name}] $msg")
-fun main(args: Array<String>) {
+fun main() {
+//sampleStart
newSingleThreadContext("Ctx1").use { ctx1 ->
newSingleThreadContext("Ctx2").use { ctx2 ->
runBlocking(ctx1) {
@@ -21,4 +22,5 @@
}
}
}
+//sampleEnd
}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-context-05.kt b/core/kotlinx-coroutines-core/test/guide/example-context-05.kt
index 0dca2a9..f39ffd9 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-context-05.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-context-05.kt
@@ -6,8 +6,9 @@
package kotlinx.coroutines.guide.context05
import kotlinx.coroutines.*
-import kotlin.coroutines.*
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
+//sampleStart
println("My job is ${coroutineContext[Job]}")
+//sampleEnd
}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-context-06.kt b/core/kotlinx-coroutines-core/test/guide/example-context-06.kt
index d4bc14b..ba0b04c 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-context-06.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-context-06.kt
@@ -6,9 +6,9 @@
package kotlinx.coroutines.guide.context06
import kotlinx.coroutines.*
-import kotlin.coroutines.*
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
+//sampleStart
// launch a coroutine to process some kind of incoming request
val request = launch {
// it spawns two other jobs, one with GlobalScope
@@ -29,4 +29,5 @@
request.cancel() // cancel processing of the request
delay(1000) // delay a second to see what happens
println("main: Who has survived request cancellation?")
+//sampleEnd
}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-context-07.kt b/core/kotlinx-coroutines-core/test/guide/example-context-07.kt
index 478e2a8..992e672 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-context-07.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-context-07.kt
@@ -6,9 +6,9 @@
package kotlinx.coroutines.guide.context07
import kotlinx.coroutines.*
-import kotlin.coroutines.*
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
+//sampleStart
// launch a coroutine to process some kind of incoming request
val request = launch {
repeat(3) { i -> // launch a few children jobs
@@ -21,4 +21,5 @@
}
request.join() // wait for completion of the request, including all its children
println("Now processing of the request is complete")
+//sampleEnd
}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-context-08.kt b/core/kotlinx-coroutines-core/test/guide/example-context-08.kt
index dd1d482..bcc754a 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-context-08.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-context-08.kt
@@ -9,7 +9,8 @@
fun log(msg: String) = println("[${Thread.currentThread().name}] $msg")
-fun main(args: Array<String>) = runBlocking(CoroutineName("main")) {
+fun main() = runBlocking(CoroutineName("main")) {
+//sampleStart
log("Started main coroutine")
// run two background value computations
val v1 = async(CoroutineName("v1coroutine")) {
@@ -23,4 +24,5 @@
6
}
log("The answer for v1 / v2 = ${v1.await() / v2.await()}")
+//sampleEnd
}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-context-09.kt b/core/kotlinx-coroutines-core/test/guide/example-context-09.kt
index 7603c46..58b1dd0 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-context-09.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-context-09.kt
@@ -6,10 +6,11 @@
package kotlinx.coroutines.guide.context09
import kotlinx.coroutines.*
-import kotlin.coroutines.*
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
+//sampleStart
launch(Dispatchers.Default + CoroutineName("test")) {
println("I'm working in thread ${Thread.currentThread().name}")
}
+//sampleEnd
}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-context-10.kt b/core/kotlinx-coroutines-core/test/guide/example-context-10.kt
index 4f8b038..61b66c4 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-context-10.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-context-10.kt
@@ -5,8 +5,8 @@
// This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit.
package kotlinx.coroutines.guide.context10
-import kotlinx.coroutines.*
import kotlin.coroutines.*
+import kotlinx.coroutines.*
class Activity : CoroutineScope {
lateinit var job: Job
@@ -37,7 +37,8 @@
}
} // class Activity ends
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
+//sampleStart
val activity = Activity()
activity.create() // create an activity
activity.doSomething() // run test function
@@ -46,4 +47,5 @@
println("Destroying activity!")
activity.destroy() // cancels all coroutines
delay(1000) // visually confirm that they don't work
+//sampleEnd
}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-context-11.kt b/core/kotlinx-coroutines-core/test/guide/example-context-11.kt
index 24282b6..8de958e 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-context-11.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-context-11.kt
@@ -6,11 +6,11 @@
package kotlinx.coroutines.guide.context11
import kotlinx.coroutines.*
-import kotlin.coroutines.*
val threadLocal = ThreadLocal<String?>() // declare thread-local variable
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
+//sampleStart
threadLocal.set("main")
println("Pre-main, current thread: ${Thread.currentThread()}, thread local value: '${threadLocal.get()}'")
val job = launch(Dispatchers.Default + threadLocal.asContextElement(value = "launch")) {
@@ -20,4 +20,5 @@
}
job.join()
println("Post-main, current thread: ${Thread.currentThread()}, thread local value: '${threadLocal.get()}'")
+//sampleEnd
}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-exceptions-01.kt b/core/kotlinx-coroutines-core/test/guide/example-exceptions-01.kt
index 3e72819..c931409 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-exceptions-01.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-exceptions-01.kt
@@ -7,7 +7,7 @@
import kotlinx.coroutines.*
-fun main(args: Array<String>) = runBlocking {
+fun main() = runBlocking {
val job = GlobalScope.launch {
println("Throwing exception from launch")
throw IndexOutOfBoundsException() // Will be printed to the console by Thread.defaultUncaughtExceptionHandler
diff --git a/core/kotlinx-coroutines-core/test/guide/example-exceptions-02.kt b/core/kotlinx-coroutines-core/test/guide/example-exceptions-02.kt
index b11d0bc..46f6dab 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-exceptions-02.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-exceptions-02.kt
@@ -7,7 +7,8 @@
import kotlinx.coroutines.*
-fun main(args: Array<String>) = runBlocking {
+fun main() = runBlocking {
+//sampleStart
val handler = CoroutineExceptionHandler { _, exception ->
println("Caught $exception")
}
@@ -18,4 +19,5 @@
throw ArithmeticException() // Nothing will be printed, relying on user to call deferred.await()
}
joinAll(job, deferred)
+//sampleEnd
}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-exceptions-03.kt b/core/kotlinx-coroutines-core/test/guide/example-exceptions-03.kt
index a1f2c4b..1166519 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-exceptions-03.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-exceptions-03.kt
@@ -6,9 +6,9 @@
package kotlinx.coroutines.guide.exceptions03
import kotlinx.coroutines.*
-import kotlin.coroutines.*
-fun main(args: Array<String>) = runBlocking {
+fun main() = runBlocking {
+//sampleStart
val job = launch {
val child = launch {
try {
@@ -25,4 +25,5 @@
println("Parent is not cancelled")
}
job.join()
+//sampleEnd
}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-exceptions-04.kt b/core/kotlinx-coroutines-core/test/guide/example-exceptions-04.kt
index 5db1e84..ab530fe 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-exceptions-04.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-exceptions-04.kt
@@ -6,9 +6,9 @@
package kotlinx.coroutines.guide.exceptions04
import kotlinx.coroutines.*
-import kotlin.coroutines.*
-fun main(args: Array<String>) = runBlocking {
+fun main() = runBlocking {
+//sampleEnd
val handler = CoroutineExceptionHandler { _, exception ->
println("Caught $exception")
}
@@ -31,4 +31,5 @@
}
}
job.join()
+//sampleEnd
}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-exceptions-05.kt b/core/kotlinx-coroutines-core/test/guide/example-exceptions-05.kt
index 2f8a114..5271873 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-exceptions-05.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-exceptions-05.kt
@@ -5,12 +5,12 @@
// This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit.
package kotlinx.coroutines.guide.exceptions05
-import kotlinx.coroutines.*
import kotlinx.coroutines.exceptions.*
-import kotlin.coroutines.*
+
+import kotlinx.coroutines.*
import java.io.*
-fun main(args: Array<String>) = runBlocking {
+fun main() = runBlocking {
val handler = CoroutineExceptionHandler { _, exception ->
println("Caught $exception with suppressed ${exception.suppressed.contentToString()}")
}
@@ -28,5 +28,5 @@
}
delay(Long.MAX_VALUE)
}
- job.join()
+ job.join()
}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-exceptions-06.kt b/core/kotlinx-coroutines-core/test/guide/example-exceptions-06.kt
index 6566168..076a097 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-exceptions-06.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-exceptions-06.kt
@@ -6,10 +6,10 @@
package kotlinx.coroutines.guide.exceptions06
import kotlinx.coroutines.*
-import kotlin.coroutines.*
import java.io.*
-fun main(args: Array<String>) = runBlocking {
+fun main() = runBlocking {
+//sampleStart
val handler = CoroutineExceptionHandler { _, exception ->
println("Caught original $exception")
}
@@ -29,4 +29,5 @@
}
}
job.join()
+//sampleEnd
}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-select-01.kt b/core/kotlinx-coroutines-core/test/guide/example-select-01.kt
index fd84c1d..f4cdd28 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-select-01.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-select-01.kt
@@ -36,7 +36,7 @@
}
}
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
val fizz = fizz()
val buzz = buzz()
repeat(7) {
diff --git a/core/kotlinx-coroutines-core/test/guide/example-select-02.kt b/core/kotlinx-coroutines-core/test/guide/example-select-02.kt
index 731fb68..ff7e5c6 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-select-02.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-select-02.kt
@@ -26,7 +26,7 @@
}
}
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
val a = produce<String> {
repeat(4) { send("Hello $it") }
}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-select-03.kt b/core/kotlinx-coroutines-core/test/guide/example-select-03.kt
index 56fb09c..35ea2eb 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-select-03.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-select-03.kt
@@ -20,7 +20,7 @@
}
}
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
val side = Channel<Int>() // allocate side channel
launch { // this is a very fast consumer for the side channel
side.consumeEach { println("Side channel has $it") }
diff --git a/core/kotlinx-coroutines-core/test/guide/example-select-04.kt b/core/kotlinx-coroutines-core/test/guide/example-select-04.kt
index aacfe7c..3662741 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-select-04.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-select-04.kt
@@ -20,7 +20,7 @@
return List(12) { asyncString(random.nextInt(1000)) }
}
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
val list = asyncStringsList()
val result = select<String> {
list.withIndex().forEach { (index, deferred) ->
diff --git a/core/kotlinx-coroutines-core/test/guide/example-select-05.kt b/core/kotlinx-coroutines-core/test/guide/example-select-05.kt
index a13d040..f7a4b9f 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-select-05.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-select-05.kt
@@ -36,7 +36,7 @@
str
}
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
val chan = Channel<Deferred<String>>() // the channel for test
launch { // launch printing coroutine
for (s in switchMapDeferreds(chan))
diff --git a/core/kotlinx-coroutines-core/test/guide/example-supervision-01.kt b/core/kotlinx-coroutines-core/test/guide/example-supervision-01.kt
index 43a8c70..eac450a 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-supervision-01.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-supervision-01.kt
@@ -6,12 +6,11 @@
package kotlinx.coroutines.guide.supervision01
import kotlinx.coroutines.*
-import kotlin.coroutines.*
-fun main(args: Array<String>) = runBlocking {
+fun main() = runBlocking {
val supervisor = SupervisorJob()
with(CoroutineScope(coroutineContext + supervisor)) {
- // launch the first child -- its exception is ignored for this example (don't do this in practise!)
+ // launch the first child -- its exception is ignored for this example (don't do this in practice!)
val firstChild = launch(CoroutineExceptionHandler { _, _ -> }) {
println("First child is failing")
throw AssertionError("First child is cancelled")
diff --git a/core/kotlinx-coroutines-core/test/guide/example-supervision-02.kt b/core/kotlinx-coroutines-core/test/guide/example-supervision-02.kt
index 28490a4..47a3525 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-supervision-02.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-supervision-02.kt
@@ -5,10 +5,10 @@
// This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit.
package kotlinx.coroutines.guide.supervision02
-import kotlinx.coroutines.*
import kotlin.coroutines.*
+import kotlinx.coroutines.*
-fun main(args: Array<String>) = runBlocking {
+fun main() = runBlocking {
try {
supervisorScope {
val child = launch {
diff --git a/core/kotlinx-coroutines-core/test/guide/example-supervision-03.kt b/core/kotlinx-coroutines-core/test/guide/example-supervision-03.kt
index a9f38a8..c50b263 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-supervision-03.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-supervision-03.kt
@@ -5,10 +5,10 @@
// This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit.
package kotlinx.coroutines.guide.supervision03
-import kotlinx.coroutines.*
import kotlin.coroutines.*
+import kotlinx.coroutines.*
-fun main(args: Array<String>) = runBlocking {
+fun main() = runBlocking {
val handler = CoroutineExceptionHandler { _, exception ->
println("Caught $exception")
}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-sync-01.kt b/core/kotlinx-coroutines-core/test/guide/example-sync-01.kt
index f7f4bbc..e380554 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-sync-01.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-sync-01.kt
@@ -25,7 +25,7 @@
var counter = 0
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
GlobalScope.massiveRun {
counter++
}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-sync-01b.kt b/core/kotlinx-coroutines-core/test/guide/example-sync-01b.kt
index 119a291..b242f30 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-sync-01b.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-sync-01b.kt
@@ -26,7 +26,7 @@
val mtContext = newFixedThreadPoolContext(2, "mtPool") // explicitly define context with two threads
var counter = 0
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
CoroutineScope(mtContext).massiveRun { // use it instead of Dispatchers.Default in this sample and below
counter++
}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-sync-02.kt b/core/kotlinx-coroutines-core/test/guide/example-sync-02.kt
index b338b17..b1c10d8 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-sync-02.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-sync-02.kt
@@ -26,7 +26,7 @@
@Volatile // in Kotlin `volatile` is an annotation
var counter = 0
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
GlobalScope.massiveRun {
counter++
}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-sync-03.kt b/core/kotlinx-coroutines-core/test/guide/example-sync-03.kt
index bfedaa1..1eabc1b 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-sync-03.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-sync-03.kt
@@ -26,7 +26,7 @@
var counter = AtomicInteger()
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
GlobalScope.massiveRun {
counter.incrementAndGet()
}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-sync-04.kt b/core/kotlinx-coroutines-core/test/guide/example-sync-04.kt
index b9dd90a..5d1fd88 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-sync-04.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-sync-04.kt
@@ -26,7 +26,7 @@
val counterContext = newSingleThreadContext("CounterContext")
var counter = 0
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
GlobalScope.massiveRun { // run each coroutine with DefaultDispathcer
withContext(counterContext) { // but confine each increment to the single-threaded context
counter++
diff --git a/core/kotlinx-coroutines-core/test/guide/example-sync-05.kt b/core/kotlinx-coroutines-core/test/guide/example-sync-05.kt
index bfe40a0..752c0e8 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-sync-05.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-sync-05.kt
@@ -26,7 +26,7 @@
val counterContext = newSingleThreadContext("CounterContext")
var counter = 0
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
CoroutineScope(counterContext).massiveRun { // run each coroutine in the single-threaded context
counter++
}
diff --git a/core/kotlinx-coroutines-core/test/guide/example-sync-06.kt b/core/kotlinx-coroutines-core/test/guide/example-sync-06.kt
index af54318..891533d 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-sync-06.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-sync-06.kt
@@ -27,7 +27,7 @@
val mutex = Mutex()
var counter = 0
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
GlobalScope.massiveRun {
mutex.withLock {
counter++
diff --git a/core/kotlinx-coroutines-core/test/guide/example-sync-07.kt b/core/kotlinx-coroutines-core/test/guide/example-sync-07.kt
index f781fc8..ccac26d 100644
--- a/core/kotlinx-coroutines-core/test/guide/example-sync-07.kt
+++ b/core/kotlinx-coroutines-core/test/guide/example-sync-07.kt
@@ -40,7 +40,7 @@
}
}
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
val counter = counterActor() // create the actor
GlobalScope.massiveRun {
counter.send(IncCounter)
diff --git a/core/kotlinx-coroutines-core/test/guide/test/BasicsGuideTest.kt b/core/kotlinx-coroutines-core/test/guide/test/BasicsGuideTest.kt
index 9c11fbd..93b49a6 100644
--- a/core/kotlinx-coroutines-core/test/guide/test/BasicsGuideTest.kt
+++ b/core/kotlinx-coroutines-core/test/guide/test/BasicsGuideTest.kt
@@ -7,7 +7,7 @@
@Test
fun testKotlinxCoroutinesGuideBasic01() {
- test("KotlinxCoroutinesGuideBasic01") { kotlinx.coroutines.guide.basic01.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesGuideBasic01") { kotlinx.coroutines.guide.basic01.main() }.verifyLines(
"Hello,",
"World!"
)
@@ -15,7 +15,7 @@
@Test
fun testKotlinxCoroutinesGuideBasic02() {
- test("KotlinxCoroutinesGuideBasic02") { kotlinx.coroutines.guide.basic02.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesGuideBasic02") { kotlinx.coroutines.guide.basic02.main() }.verifyLines(
"Hello,",
"World!"
)
@@ -23,7 +23,7 @@
@Test
fun testKotlinxCoroutinesGuideBasic02b() {
- test("KotlinxCoroutinesGuideBasic02b") { kotlinx.coroutines.guide.basic02b.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesGuideBasic02b") { kotlinx.coroutines.guide.basic02b.main() }.verifyLines(
"Hello,",
"World!"
)
@@ -31,7 +31,7 @@
@Test
fun testKotlinxCoroutinesGuideBasic03() {
- test("KotlinxCoroutinesGuideBasic03") { kotlinx.coroutines.guide.basic03.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesGuideBasic03") { kotlinx.coroutines.guide.basic03.main() }.verifyLines(
"Hello,",
"World!"
)
@@ -39,7 +39,7 @@
@Test
fun testKotlinxCoroutinesGuideBasic03s() {
- test("KotlinxCoroutinesGuideBasic03s") { kotlinx.coroutines.guide.basic03s.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesGuideBasic03s") { kotlinx.coroutines.guide.basic03s.main() }.verifyLines(
"Hello,",
"World!"
)
@@ -47,7 +47,7 @@
@Test
fun testKotlinxCoroutinesGuideBasic04() {
- test("KotlinxCoroutinesGuideBasic04") { kotlinx.coroutines.guide.basic04.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesGuideBasic04") { kotlinx.coroutines.guide.basic04.main() }.verifyLines(
"Task from coroutine scope",
"Task from runBlocking",
"Task from nested launch",
@@ -57,7 +57,7 @@
@Test
fun testKotlinxCoroutinesGuideBasic05() {
- test("KotlinxCoroutinesGuideBasic05") { kotlinx.coroutines.guide.basic05.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesGuideBasic05") { kotlinx.coroutines.guide.basic05.main() }.verifyLines(
"Hello,",
"World!"
)
@@ -65,14 +65,14 @@
@Test
fun testKotlinxCoroutinesGuideBasic06() {
- test("KotlinxCoroutinesGuideBasic06") { kotlinx.coroutines.guide.basic06.main(emptyArray()) }.also { lines ->
+ test("KotlinxCoroutinesGuideBasic06") { kotlinx.coroutines.guide.basic06.main() }.also { lines ->
check(lines.size == 1 && lines[0] == ".".repeat(100_000))
}
}
@Test
fun testKotlinxCoroutinesGuideBasic07() {
- test("KotlinxCoroutinesGuideBasic07") { kotlinx.coroutines.guide.basic07.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesGuideBasic07") { kotlinx.coroutines.guide.basic07.main() }.verifyLines(
"I'm sleeping 0 ...",
"I'm sleeping 1 ...",
"I'm sleeping 2 ..."
diff --git a/core/kotlinx-coroutines-core/test/guide/test/CancellationTimeOutsGuideTest.kt b/core/kotlinx-coroutines-core/test/guide/test/CancellationTimeOutsGuideTest.kt
index 47f5e88..47cc262 100644
--- a/core/kotlinx-coroutines-core/test/guide/test/CancellationTimeOutsGuideTest.kt
+++ b/core/kotlinx-coroutines-core/test/guide/test/CancellationTimeOutsGuideTest.kt
@@ -7,7 +7,7 @@
@Test
fun testKotlinxCoroutinesGuideCancel01() {
- test("KotlinxCoroutinesGuideCancel01") { kotlinx.coroutines.guide.cancel01.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesGuideCancel01") { kotlinx.coroutines.guide.cancel01.main() }.verifyLines(
"I'm sleeping 0 ...",
"I'm sleeping 1 ...",
"I'm sleeping 2 ...",
@@ -18,7 +18,7 @@
@Test
fun testKotlinxCoroutinesGuideCancel02() {
- test("KotlinxCoroutinesGuideCancel02") { kotlinx.coroutines.guide.cancel02.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesGuideCancel02") { kotlinx.coroutines.guide.cancel02.main() }.verifyLines(
"I'm sleeping 0 ...",
"I'm sleeping 1 ...",
"I'm sleeping 2 ...",
@@ -31,7 +31,7 @@
@Test
fun testKotlinxCoroutinesGuideCancel03() {
- test("KotlinxCoroutinesGuideCancel03") { kotlinx.coroutines.guide.cancel03.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesGuideCancel03") { kotlinx.coroutines.guide.cancel03.main() }.verifyLines(
"I'm sleeping 0 ...",
"I'm sleeping 1 ...",
"I'm sleeping 2 ...",
@@ -42,7 +42,7 @@
@Test
fun testKotlinxCoroutinesGuideCancel04() {
- test("KotlinxCoroutinesGuideCancel04") { kotlinx.coroutines.guide.cancel04.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesGuideCancel04") { kotlinx.coroutines.guide.cancel04.main() }.verifyLines(
"I'm sleeping 0 ...",
"I'm sleeping 1 ...",
"I'm sleeping 2 ...",
@@ -54,7 +54,7 @@
@Test
fun testKotlinxCoroutinesGuideCancel05() {
- test("KotlinxCoroutinesGuideCancel05") { kotlinx.coroutines.guide.cancel05.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesGuideCancel05") { kotlinx.coroutines.guide.cancel05.main() }.verifyLines(
"I'm sleeping 0 ...",
"I'm sleeping 1 ...",
"I'm sleeping 2 ...",
@@ -67,7 +67,7 @@
@Test
fun testKotlinxCoroutinesGuideCancel06() {
- test("KotlinxCoroutinesGuideCancel06") { kotlinx.coroutines.guide.cancel06.main(emptyArray()) }.verifyLinesStartWith(
+ test("KotlinxCoroutinesGuideCancel06") { kotlinx.coroutines.guide.cancel06.main() }.verifyLinesStartWith(
"I'm sleeping 0 ...",
"I'm sleeping 1 ...",
"I'm sleeping 2 ...",
@@ -77,7 +77,7 @@
@Test
fun testKotlinxCoroutinesGuideCancel07() {
- test("KotlinxCoroutinesGuideCancel07") { kotlinx.coroutines.guide.cancel07.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesGuideCancel07") { kotlinx.coroutines.guide.cancel07.main() }.verifyLines(
"I'm sleeping 0 ...",
"I'm sleeping 1 ...",
"I'm sleeping 2 ...",
diff --git a/core/kotlinx-coroutines-core/test/guide/test/ChannelsGuideTest.kt b/core/kotlinx-coroutines-core/test/guide/test/ChannelsGuideTest.kt
index 5f6323c..a747c98 100644
--- a/core/kotlinx-coroutines-core/test/guide/test/ChannelsGuideTest.kt
+++ b/core/kotlinx-coroutines-core/test/guide/test/ChannelsGuideTest.kt
@@ -7,7 +7,7 @@
@Test
fun testKotlinxCoroutinesGuideChannel01() {
- test("KotlinxCoroutinesGuideChannel01") { kotlinx.coroutines.guide.channel01.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesGuideChannel01") { kotlinx.coroutines.guide.channel01.main() }.verifyLines(
"1",
"4",
"9",
@@ -19,7 +19,7 @@
@Test
fun testKotlinxCoroutinesGuideChannel02() {
- test("KotlinxCoroutinesGuideChannel02") { kotlinx.coroutines.guide.channel02.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesGuideChannel02") { kotlinx.coroutines.guide.channel02.main() }.verifyLines(
"1",
"4",
"9",
@@ -31,7 +31,7 @@
@Test
fun testKotlinxCoroutinesGuideChannel03() {
- test("KotlinxCoroutinesGuideChannel03") { kotlinx.coroutines.guide.channel03.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesGuideChannel03") { kotlinx.coroutines.guide.channel03.main() }.verifyLines(
"1",
"4",
"9",
@@ -43,7 +43,7 @@
@Test
fun testKotlinxCoroutinesGuideChannel04() {
- test("KotlinxCoroutinesGuideChannel04") { kotlinx.coroutines.guide.channel04.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesGuideChannel04") { kotlinx.coroutines.guide.channel04.main() }.verifyLines(
"1",
"4",
"9",
@@ -55,7 +55,7 @@
@Test
fun testKotlinxCoroutinesGuideChannel05() {
- test("KotlinxCoroutinesGuideChannel05") { kotlinx.coroutines.guide.channel05.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesGuideChannel05") { kotlinx.coroutines.guide.channel05.main() }.verifyLines(
"2",
"3",
"5",
@@ -71,14 +71,14 @@
@Test
fun testKotlinxCoroutinesGuideChannel06() {
- test("KotlinxCoroutinesGuideChannel06") { kotlinx.coroutines.guide.channel06.main(emptyArray()) }.also { lines ->
+ test("KotlinxCoroutinesGuideChannel06") { kotlinx.coroutines.guide.channel06.main() }.also { lines ->
check(lines.size == 10 && lines.withIndex().all { (i, line) -> line.startsWith("Processor #") && line.endsWith(" received ${i + 1}") })
}
}
@Test
fun testKotlinxCoroutinesGuideChannel07() {
- test("KotlinxCoroutinesGuideChannel07") { kotlinx.coroutines.guide.channel07.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesGuideChannel07") { kotlinx.coroutines.guide.channel07.main() }.verifyLines(
"foo",
"foo",
"BAR!",
@@ -90,7 +90,7 @@
@Test
fun testKotlinxCoroutinesGuideChannel08() {
- test("KotlinxCoroutinesGuideChannel08") { kotlinx.coroutines.guide.channel08.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesGuideChannel08") { kotlinx.coroutines.guide.channel08.main() }.verifyLines(
"Sending 0",
"Sending 1",
"Sending 2",
@@ -101,7 +101,7 @@
@Test
fun testKotlinxCoroutinesGuideChannel09() {
- test("KotlinxCoroutinesGuideChannel09") { kotlinx.coroutines.guide.channel09.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesGuideChannel09") { kotlinx.coroutines.guide.channel09.main() }.verifyLines(
"ping Ball(hits=1)",
"pong Ball(hits=2)",
"ping Ball(hits=3)",
@@ -111,7 +111,7 @@
@Test
fun testKotlinxCoroutinesGuideChannel10() {
- test("KotlinxCoroutinesGuideChannel10") { kotlinx.coroutines.guide.channel10.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesGuideChannel10") { kotlinx.coroutines.guide.channel10.main() }.verifyLines(
"Initial element is available immediately: kotlin.Unit",
"Next element is not ready in 50 ms: null",
"Next element is ready in 100 ms: kotlin.Unit",
diff --git a/core/kotlinx-coroutines-core/test/guide/test/ComposingGuideTest.kt b/core/kotlinx-coroutines-core/test/guide/test/ComposingGuideTest.kt
index c44dbba..de4cba4 100644
--- a/core/kotlinx-coroutines-core/test/guide/test/ComposingGuideTest.kt
+++ b/core/kotlinx-coroutines-core/test/guide/test/ComposingGuideTest.kt
@@ -7,7 +7,7 @@
@Test
fun testKotlinxCoroutinesGuideCompose01() {
- test("KotlinxCoroutinesGuideCompose01") { kotlinx.coroutines.guide.compose01.main(emptyArray()) }.verifyLinesArbitraryTime(
+ test("KotlinxCoroutinesGuideCompose01") { kotlinx.coroutines.guide.compose01.main() }.verifyLinesArbitraryTime(
"The answer is 42",
"Completed in 2017 ms"
)
@@ -15,7 +15,7 @@
@Test
fun testKotlinxCoroutinesGuideCompose02() {
- test("KotlinxCoroutinesGuideCompose02") { kotlinx.coroutines.guide.compose02.main(emptyArray()) }.verifyLinesArbitraryTime(
+ test("KotlinxCoroutinesGuideCompose02") { kotlinx.coroutines.guide.compose02.main() }.verifyLinesArbitraryTime(
"The answer is 42",
"Completed in 1017 ms"
)
@@ -23,7 +23,7 @@
@Test
fun testKotlinxCoroutinesGuideCompose03() {
- test("KotlinxCoroutinesGuideCompose03") { kotlinx.coroutines.guide.compose03.main(emptyArray()) }.verifyLinesArbitraryTime(
+ test("KotlinxCoroutinesGuideCompose03") { kotlinx.coroutines.guide.compose03.main() }.verifyLinesArbitraryTime(
"The answer is 42",
"Completed in 1017 ms"
)
@@ -31,7 +31,7 @@
@Test
fun testKotlinxCoroutinesGuideCompose04() {
- test("KotlinxCoroutinesGuideCompose04") { kotlinx.coroutines.guide.compose04.main(emptyArray()) }.verifyLinesArbitraryTime(
+ test("KotlinxCoroutinesGuideCompose04") { kotlinx.coroutines.guide.compose04.main() }.verifyLinesArbitraryTime(
"The answer is 42",
"Completed in 1085 ms"
)
@@ -39,7 +39,7 @@
@Test
fun testKotlinxCoroutinesGuideCompose05() {
- test("KotlinxCoroutinesGuideCompose05") { kotlinx.coroutines.guide.compose05.main(emptyArray()) }.verifyLinesArbitraryTime(
+ test("KotlinxCoroutinesGuideCompose05") { kotlinx.coroutines.guide.compose05.main() }.verifyLinesArbitraryTime(
"The answer is 42",
"Completed in 1017 ms"
)
@@ -47,7 +47,7 @@
@Test
fun testKotlinxCoroutinesGuideCompose06() {
- test("KotlinxCoroutinesGuideCompose06") { kotlinx.coroutines.guide.compose06.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesGuideCompose06") { kotlinx.coroutines.guide.compose06.main() }.verifyLines(
"Second child throws an exception",
"First child was cancelled",
"Computation failed with ArithmeticException"
diff --git a/core/kotlinx-coroutines-core/test/guide/test/DispatcherGuideTest.kt b/core/kotlinx-coroutines-core/test/guide/test/DispatcherGuideTest.kt
index 66d5b05..180738f 100644
--- a/core/kotlinx-coroutines-core/test/guide/test/DispatcherGuideTest.kt
+++ b/core/kotlinx-coroutines-core/test/guide/test/DispatcherGuideTest.kt
@@ -7,7 +7,7 @@
@Test
fun testKotlinxCoroutinesGuideContext01() {
- test("KotlinxCoroutinesGuideContext01") { kotlinx.coroutines.guide.context01.main(emptyArray()) }.verifyLinesStartUnordered(
+ test("KotlinxCoroutinesGuideContext01") { kotlinx.coroutines.guide.context01.main() }.verifyLinesStartUnordered(
"Unconfined : I'm working in thread main",
"Default : I'm working in thread DefaultDispatcher-worker-1",
"newSingleThreadContext: I'm working in thread MyOwnThread",
@@ -17,7 +17,7 @@
@Test
fun testKotlinxCoroutinesGuideContext02() {
- test("KotlinxCoroutinesGuideContext02") { kotlinx.coroutines.guide.context02.main(emptyArray()) }.verifyLinesStart(
+ test("KotlinxCoroutinesGuideContext02") { kotlinx.coroutines.guide.context02.main() }.verifyLinesStart(
"Unconfined : I'm working in thread main",
"main runBlocking: I'm working in thread main",
"Unconfined : After delay in thread kotlinx.coroutines.DefaultExecutor",
@@ -27,7 +27,7 @@
@Test
fun testKotlinxCoroutinesGuideContext03() {
- test("KotlinxCoroutinesGuideContext03") { kotlinx.coroutines.guide.context03.main(emptyArray()) }.verifyLinesFlexibleThread(
+ test("KotlinxCoroutinesGuideContext03") { kotlinx.coroutines.guide.context03.main() }.verifyLinesFlexibleThread(
"[main @coroutine#2] I'm computing a piece of the answer",
"[main @coroutine#3] I'm computing another piece of the answer",
"[main @coroutine#1] The answer is 42"
@@ -36,7 +36,7 @@
@Test
fun testKotlinxCoroutinesGuideContext04() {
- test("KotlinxCoroutinesGuideContext04") { kotlinx.coroutines.guide.context04.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesGuideContext04") { kotlinx.coroutines.guide.context04.main() }.verifyLines(
"[Ctx1 @coroutine#1] Started in ctx1",
"[Ctx2 @coroutine#1] Working in ctx2",
"[Ctx1 @coroutine#1] Back to ctx1"
@@ -45,14 +45,14 @@
@Test
fun testKotlinxCoroutinesGuideContext05() {
- test("KotlinxCoroutinesGuideContext05") { kotlinx.coroutines.guide.context05.main(emptyArray()) }.also { lines ->
+ test("KotlinxCoroutinesGuideContext05") { kotlinx.coroutines.guide.context05.main() }.also { lines ->
check(lines.size == 1 && lines[0].startsWith("My job is \"coroutine#1\":BlockingCoroutine{Active}@"))
}
}
@Test
fun testKotlinxCoroutinesGuideContext06() {
- test("KotlinxCoroutinesGuideContext06") { kotlinx.coroutines.guide.context06.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesGuideContext06") { kotlinx.coroutines.guide.context06.main() }.verifyLines(
"job1: I run in GlobalScope and execute independently!",
"job2: I am a child of the request coroutine",
"job1: I am not affected by cancellation of the request",
@@ -62,7 +62,7 @@
@Test
fun testKotlinxCoroutinesGuideContext07() {
- test("KotlinxCoroutinesGuideContext07") { kotlinx.coroutines.guide.context07.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesGuideContext07") { kotlinx.coroutines.guide.context07.main() }.verifyLines(
"request: I'm done and I don't explicitly join my children that are still active",
"Coroutine 0 is done",
"Coroutine 1 is done",
@@ -73,7 +73,7 @@
@Test
fun testKotlinxCoroutinesGuideContext08() {
- test("KotlinxCoroutinesGuideContext08") { kotlinx.coroutines.guide.context08.main(emptyArray()) }.verifyLinesFlexibleThread(
+ test("KotlinxCoroutinesGuideContext08") { kotlinx.coroutines.guide.context08.main() }.verifyLinesFlexibleThread(
"[main @main#1] Started main coroutine",
"[main @v1coroutine#2] Computing v1",
"[main @v2coroutine#3] Computing v2",
@@ -83,14 +83,14 @@
@Test
fun testKotlinxCoroutinesGuideContext09() {
- test("KotlinxCoroutinesGuideContext09") { kotlinx.coroutines.guide.context09.main(emptyArray()) }.verifyLinesFlexibleThread(
+ test("KotlinxCoroutinesGuideContext09") { kotlinx.coroutines.guide.context09.main() }.verifyLinesFlexibleThread(
"I'm working in thread DefaultDispatcher-worker-1 @test#2"
)
}
@Test
fun testKotlinxCoroutinesGuideContext10() {
- test("KotlinxCoroutinesGuideContext10") { kotlinx.coroutines.guide.context10.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesGuideContext10") { kotlinx.coroutines.guide.context10.main() }.verifyLines(
"Launched coroutines",
"Coroutine 0 is done",
"Coroutine 1 is done",
@@ -100,7 +100,7 @@
@Test
fun testKotlinxCoroutinesGuideContext11() {
- test("KotlinxCoroutinesGuideContext11") { kotlinx.coroutines.guide.context11.main(emptyArray()) }.verifyLinesFlexibleThread(
+ test("KotlinxCoroutinesGuideContext11") { kotlinx.coroutines.guide.context11.main() }.verifyLinesFlexibleThread(
"Pre-main, current thread: Thread[main @coroutine#1,5,main], thread local value: 'main'",
"Launch start, current thread: Thread[DefaultDispatcher-worker-1 @coroutine#2,5,main], thread local value: 'launch'",
"After yield, current thread: Thread[DefaultDispatcher-worker-2 @coroutine#2,5,main], thread local value: 'launch'",
diff --git a/core/kotlinx-coroutines-core/test/guide/test/ExceptionsGuideTest.kt b/core/kotlinx-coroutines-core/test/guide/test/ExceptionsGuideTest.kt
index 305d016..7fa692b 100644
--- a/core/kotlinx-coroutines-core/test/guide/test/ExceptionsGuideTest.kt
+++ b/core/kotlinx-coroutines-core/test/guide/test/ExceptionsGuideTest.kt
@@ -7,7 +7,7 @@
@Test
fun testKotlinxCoroutinesGuideExceptions01() {
- test("KotlinxCoroutinesGuideExceptions01") { kotlinx.coroutines.guide.exceptions01.main(emptyArray()) }.verifyExceptions(
+ test("KotlinxCoroutinesGuideExceptions01") { kotlinx.coroutines.guide.exceptions01.main() }.verifyExceptions(
"Throwing exception from launch",
"Exception in thread \"DefaultDispatcher-worker-2 @coroutine#2\" java.lang.IndexOutOfBoundsException",
"Joined failed job",
@@ -18,14 +18,14 @@
@Test
fun testKotlinxCoroutinesGuideExceptions02() {
- test("KotlinxCoroutinesGuideExceptions02") { kotlinx.coroutines.guide.exceptions02.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesGuideExceptions02") { kotlinx.coroutines.guide.exceptions02.main() }.verifyLines(
"Caught java.lang.AssertionError"
)
}
@Test
fun testKotlinxCoroutinesGuideExceptions03() {
- test("KotlinxCoroutinesGuideExceptions03") { kotlinx.coroutines.guide.exceptions03.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesGuideExceptions03") { kotlinx.coroutines.guide.exceptions03.main() }.verifyLines(
"Cancelling child",
"Child is cancelled",
"Parent is not cancelled"
@@ -34,7 +34,7 @@
@Test
fun testKotlinxCoroutinesGuideExceptions04() {
- test("KotlinxCoroutinesGuideExceptions04") { kotlinx.coroutines.guide.exceptions04.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesGuideExceptions04") { kotlinx.coroutines.guide.exceptions04.main() }.verifyLines(
"Second child throws an exception",
"Children are cancelled, but exception is not handled until all children terminate",
"The first child finished its non cancellable block",
@@ -44,14 +44,14 @@
@Test
fun testKotlinxCoroutinesGuideExceptions05() {
- test("KotlinxCoroutinesGuideExceptions05") { kotlinx.coroutines.guide.exceptions05.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesGuideExceptions05") { kotlinx.coroutines.guide.exceptions05.main() }.verifyLines(
"Caught java.io.IOException with suppressed [java.lang.ArithmeticException]"
)
}
@Test
fun testKotlinxCoroutinesGuideExceptions06() {
- test("KotlinxCoroutinesGuideExceptions06") { kotlinx.coroutines.guide.exceptions06.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesGuideExceptions06") { kotlinx.coroutines.guide.exceptions06.main() }.verifyLines(
"Rethrowing CancellationException with original cause",
"Caught original java.io.IOException"
)
@@ -59,7 +59,7 @@
@Test
fun testKotlinxCoroutinesGuideSupervision01() {
- test("KotlinxCoroutinesGuideSupervision01") { kotlinx.coroutines.guide.supervision01.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesGuideSupervision01") { kotlinx.coroutines.guide.supervision01.main() }.verifyLines(
"First child is failing",
"First child is cancelled: true, but second one is still active",
"Cancelling supervisor",
@@ -69,7 +69,7 @@
@Test
fun testKotlinxCoroutinesGuideSupervision02() {
- test("KotlinxCoroutinesGuideSupervision02") { kotlinx.coroutines.guide.supervision02.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesGuideSupervision02") { kotlinx.coroutines.guide.supervision02.main() }.verifyLines(
"Child is sleeping",
"Throwing exception from scope",
"Child is cancelled",
@@ -79,7 +79,7 @@
@Test
fun testKotlinxCoroutinesGuideSupervision03() {
- test("KotlinxCoroutinesGuideSupervision03") { kotlinx.coroutines.guide.supervision03.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesGuideSupervision03") { kotlinx.coroutines.guide.supervision03.main() }.verifyLines(
"Scope is completing",
"Child throws an exception",
"Caught java.lang.AssertionError",
diff --git a/core/kotlinx-coroutines-core/test/guide/test/SelectGuideTest.kt b/core/kotlinx-coroutines-core/test/guide/test/SelectGuideTest.kt
index 990b87e..b5246ff 100644
--- a/core/kotlinx-coroutines-core/test/guide/test/SelectGuideTest.kt
+++ b/core/kotlinx-coroutines-core/test/guide/test/SelectGuideTest.kt
@@ -7,7 +7,7 @@
@Test
fun testKotlinxCoroutinesGuideSelect01() {
- test("KotlinxCoroutinesGuideSelect01") { kotlinx.coroutines.guide.select01.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesGuideSelect01") { kotlinx.coroutines.guide.select01.main() }.verifyLines(
"fizz -> 'Fizz'",
"buzz -> 'Buzz!'",
"fizz -> 'Fizz'",
@@ -20,7 +20,7 @@
@Test
fun testKotlinxCoroutinesGuideSelect02() {
- test("KotlinxCoroutinesGuideSelect02") { kotlinx.coroutines.guide.select02.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesGuideSelect02") { kotlinx.coroutines.guide.select02.main() }.verifyLines(
"a -> 'Hello 0'",
"a -> 'Hello 1'",
"b -> 'World 0'",
@@ -34,7 +34,7 @@
@Test
fun testKotlinxCoroutinesGuideSelect03() {
- test("KotlinxCoroutinesGuideSelect03") { kotlinx.coroutines.guide.select03.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesGuideSelect03") { kotlinx.coroutines.guide.select03.main() }.verifyLines(
"Consuming 1",
"Side channel has 2",
"Side channel has 3",
@@ -51,7 +51,7 @@
@Test
fun testKotlinxCoroutinesGuideSelect04() {
- test("KotlinxCoroutinesGuideSelect04") { kotlinx.coroutines.guide.select04.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesGuideSelect04") { kotlinx.coroutines.guide.select04.main() }.verifyLines(
"Deferred 4 produced answer 'Waited for 128 ms'",
"11 coroutines are still active"
)
@@ -59,7 +59,7 @@
@Test
fun testKotlinxCoroutinesGuideSelect05() {
- test("KotlinxCoroutinesGuideSelect05") { kotlinx.coroutines.guide.select05.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesGuideSelect05") { kotlinx.coroutines.guide.select05.main() }.verifyLines(
"BEGIN",
"Replace",
"END",
diff --git a/core/kotlinx-coroutines-core/test/guide/test/SharedStateGuideTest.kt b/core/kotlinx-coroutines-core/test/guide/test/SharedStateGuideTest.kt
index a18da1e..b57dcfd 100644
--- a/core/kotlinx-coroutines-core/test/guide/test/SharedStateGuideTest.kt
+++ b/core/kotlinx-coroutines-core/test/guide/test/SharedStateGuideTest.kt
@@ -7,7 +7,7 @@
@Test
fun testKotlinxCoroutinesGuideSync01() {
- test("KotlinxCoroutinesGuideSync01") { kotlinx.coroutines.guide.sync01.main(emptyArray()) }.verifyLinesStart(
+ test("KotlinxCoroutinesGuideSync01") { kotlinx.coroutines.guide.sync01.main() }.verifyLinesStart(
"Completed 100000 actions in",
"Counter ="
)
@@ -15,7 +15,7 @@
@Test
fun testKotlinxCoroutinesGuideSync01b() {
- test("KotlinxCoroutinesGuideSync01b") { kotlinx.coroutines.guide.sync01b.main(emptyArray()) }.verifyLinesStart(
+ test("KotlinxCoroutinesGuideSync01b") { kotlinx.coroutines.guide.sync01b.main() }.verifyLinesStart(
"Completed 100000 actions in",
"Counter ="
)
@@ -23,7 +23,7 @@
@Test
fun testKotlinxCoroutinesGuideSync02() {
- test("KotlinxCoroutinesGuideSync02") { kotlinx.coroutines.guide.sync02.main(emptyArray()) }.verifyLinesStart(
+ test("KotlinxCoroutinesGuideSync02") { kotlinx.coroutines.guide.sync02.main() }.verifyLinesStart(
"Completed 100000 actions in",
"Counter ="
)
@@ -31,7 +31,7 @@
@Test
fun testKotlinxCoroutinesGuideSync03() {
- test("KotlinxCoroutinesGuideSync03") { kotlinx.coroutines.guide.sync03.main(emptyArray()) }.verifyLinesArbitraryTime(
+ test("KotlinxCoroutinesGuideSync03") { kotlinx.coroutines.guide.sync03.main() }.verifyLinesArbitraryTime(
"Completed 100000 actions in xxx ms",
"Counter = 100000"
)
@@ -39,7 +39,7 @@
@Test
fun testKotlinxCoroutinesGuideSync04() {
- test("KotlinxCoroutinesGuideSync04") { kotlinx.coroutines.guide.sync04.main(emptyArray()) }.verifyLinesArbitraryTime(
+ test("KotlinxCoroutinesGuideSync04") { kotlinx.coroutines.guide.sync04.main() }.verifyLinesArbitraryTime(
"Completed 100000 actions in xxx ms",
"Counter = 100000"
)
@@ -47,7 +47,7 @@
@Test
fun testKotlinxCoroutinesGuideSync05() {
- test("KotlinxCoroutinesGuideSync05") { kotlinx.coroutines.guide.sync05.main(emptyArray()) }.verifyLinesArbitraryTime(
+ test("KotlinxCoroutinesGuideSync05") { kotlinx.coroutines.guide.sync05.main() }.verifyLinesArbitraryTime(
"Completed 100000 actions in xxx ms",
"Counter = 100000"
)
@@ -55,7 +55,7 @@
@Test
fun testKotlinxCoroutinesGuideSync06() {
- test("KotlinxCoroutinesGuideSync06") { kotlinx.coroutines.guide.sync06.main(emptyArray()) }.verifyLinesArbitraryTime(
+ test("KotlinxCoroutinesGuideSync06") { kotlinx.coroutines.guide.sync06.main() }.verifyLinesArbitraryTime(
"Completed 100000 actions in xxx ms",
"Counter = 100000"
)
@@ -63,7 +63,7 @@
@Test
fun testKotlinxCoroutinesGuideSync07() {
- test("KotlinxCoroutinesGuideSync07") { kotlinx.coroutines.guide.sync07.main(emptyArray()) }.verifyLinesArbitraryTime(
+ test("KotlinxCoroutinesGuideSync07") { kotlinx.coroutines.guide.sync07.main() }.verifyLinesArbitraryTime(
"Completed 100000 actions in xxx ms",
"Counter = 100000"
)
diff --git a/docs/basics.md b/docs/basics.md
index f994469..fb0adb8 100644
--- a/docs/basics.md
+++ b/docs/basics.md
@@ -5,8 +5,6 @@
// This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit.
package kotlinx.coroutines.guide.$$1$$2
-
-import kotlinx.coroutines.*
-->
<!--- KNIT ../core/kotlinx-coroutines-core/test/guide/.*\.kt -->
<!--- TEST_OUT ../core/kotlinx-coroutines-core/test/guide/test/BasicsGuideTest.kt
@@ -43,10 +41,12 @@
Run the following code:
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) {
+import kotlinx.coroutines.*
+
+fun main() {
GlobalScope.launch { // launch new coroutine in background and continue
delay(1000L) // non-blocking delay for 1 second (default time unit is ms)
println("World!") // print after delay
@@ -92,10 +92,12 @@
It is easy to get lost which one is blocking and which one is not.
Let's be explicit about blocking using [runBlocking] coroutine builder:
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) {
+import kotlinx.coroutines.*
+
+fun main() {
GlobalScope.launch { // launch new coroutine in background and continue
delay(1000L)
println("World!")
@@ -122,10 +124,12 @@
This example can be also rewritten in a more idiomatic way, using `runBlocking` to wrap
the execution of the main function:
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking<Unit> { // start main coroutine
+import kotlinx.coroutines.*
+
+fun main() = runBlocking<Unit> { // start main coroutine
GlobalScope.launch { // launch new coroutine in background and continue
delay(1000L)
println("World!")
@@ -149,6 +153,10 @@
This is also a way to write unit-tests for suspending functions:
+<!--- INCLUDE
+import kotlinx.coroutines.*
+-->
+
<div class="sample" markdown="1" theme="idea" data-highlight-only>
```kotlin
@@ -169,16 +177,20 @@
Delaying for a time while another coroutine is working is not a good approach. Let's explicitly
wait (in a non-blocking way) until the background [Job] that we have launched is complete:
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking {
+import kotlinx.coroutines.*
+
+fun main() = runBlocking {
+//sampleStart
val job = GlobalScope.launch { // launch new coroutine and keep a reference to its Job
delay(1000L)
println("World!")
}
println("Hello,")
job.join() // wait until child coroutine completes
+//sampleEnd
}
```
@@ -213,10 +225,12 @@
an outer coroutine (`runBlocking` in our example) does not complete until all the coroutines launched
in its scope complete. Thus, we can make our example simpler:
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking { // this: CoroutineScope
+import kotlinx.coroutines.*
+
+fun main() = runBlocking { // this: CoroutineScope
launch { // launch new coroutine in the scope of runBlocking
delay(1000L)
println("World!")
@@ -240,10 +254,12 @@
complete. The main difference between [runBlocking] and [coroutineScope] is that the latter does not block the current thread
while waiting for all children to complete.
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking { // this: CoroutineScope
+import kotlinx.coroutines.*
+
+fun main() = runBlocking { // this: CoroutineScope
launch {
delay(200L)
println("Task from runBlocking")
@@ -282,10 +298,12 @@
just like regular functions, but their additional feature is that they can, in turn,
use other suspending functions, like `delay` in this example, to _suspend_ execution of a coroutine.
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking {
+import kotlinx.coroutines.*
+
+fun main() = runBlocking {
launch { doWorld() }
println("Hello,")
}
@@ -322,7 +340,9 @@
<div class="sample" markdown="1" theme="idea" data-highlight-only>
```kotlin
-fun main(args: Array<String>) = runBlocking {
+import kotlinx.coroutines.*
+
+fun main() = runBlocking {
repeat(100_000) { // launch a lot of coroutines
launch {
delay(1000L)
@@ -346,10 +366,13 @@
The following code launches a long-running coroutine in [GlobalScope] that prints "I'm sleeping" twice a second and then
returns from the main function after some delay:
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking {
+import kotlinx.coroutines.*
+
+fun main() = runBlocking {
+//sampleStart
GlobalScope.launch {
repeat(1000) { i ->
println("I'm sleeping $i ...")
@@ -357,6 +380,7 @@
}
}
delay(1300L) // just quit after delay
+//sampleEnd
}
```
diff --git a/docs/cancellation-and-timeouts.md b/docs/cancellation-and-timeouts.md
index c785429..77df285 100644
--- a/docs/cancellation-and-timeouts.md
+++ b/docs/cancellation-and-timeouts.md
@@ -5,8 +5,6 @@
// This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit.
package kotlinx.coroutines.guide.$$1$$2
-
-import kotlinx.coroutines.*
-->
<!--- KNIT ../core/kotlinx-coroutines-core/test/guide/.*\.kt -->
<!--- TEST_OUT ../core/kotlinx-coroutines-core/test/guide/test/CancellationTimeOutsGuideTest.kt
@@ -42,10 +40,13 @@
is no longer needed and its operation can be cancelled.
The [launch] function returns a [Job] that can be used to cancel running coroutine:
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking {
+import kotlinx.coroutines.*
+
+fun main() = runBlocking {
+//sampleStart
val job = launch {
repeat(1000) { i ->
println("I'm sleeping $i ...")
@@ -57,6 +58,7 @@
job.cancel() // cancels the job
job.join() // waits for job's completion
println("main: Now I can quit.")
+//sampleEnd
}
```
@@ -88,10 +90,13 @@
a computation and does not check for cancellation, then it cannot be cancelled, like the following
example shows:
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking {
+import kotlinx.coroutines.*
+
+fun main() = runBlocking {
+//sampleStart
val startTime = System.currentTimeMillis()
val job = launch(Dispatchers.Default) {
var nextPrintTime = startTime
@@ -108,6 +113,7 @@
println("main: I'm tired of waiting!")
job.cancelAndJoin() // cancels the job and waits for its completion
println("main: Now I can quit.")
+//sampleEnd
}
```
@@ -136,10 +142,13 @@
Replace `while (i < 5)` in the previous example with `while (isActive)` and rerun it.
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking {
+import kotlinx.coroutines.*
+
+fun main() = runBlocking {
+//sampleStart
val startTime = System.currentTimeMillis()
val job = launch(Dispatchers.Default) {
var nextPrintTime = startTime
@@ -156,6 +165,7 @@
println("main: I'm tired of waiting!")
job.cancelAndJoin() // cancels the job and waits for its completion
println("main: Now I can quit.")
+//sampleEnd
}
```
@@ -181,10 +191,13 @@
finalization actions normally when coroutine is cancelled:
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking {
+import kotlinx.coroutines.*
+
+fun main() = runBlocking {
+//sampleStart
val job = launch {
try {
repeat(1000) { i ->
@@ -199,6 +212,7 @@
println("main: I'm tired of waiting!")
job.cancelAndJoin() // cancels the job and waits for its completion
println("main: Now I can quit.")
+//sampleEnd
}
```
@@ -229,10 +243,13 @@
rare case when you need to suspend in the cancelled coroutine you can wrap the corresponding code in
`withContext(NonCancellable) {...}` using [withContext] function and [NonCancellable] context as the following example shows:
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking {
+import kotlinx.coroutines.*
+
+fun main() = runBlocking {
+//sampleStart
val job = launch {
try {
repeat(1000) { i ->
@@ -251,6 +268,7 @@
println("main: I'm tired of waiting!")
job.cancelAndJoin() // cancels the job and waits for its completion
println("main: Now I can quit.")
+//sampleEnd
}
```
@@ -276,16 +294,20 @@
the tracked one after delay, there is a ready to use [withTimeout] function that does it.
Look at the following example:
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking {
+import kotlinx.coroutines.*
+
+fun main() = runBlocking {
+//sampleStart
withTimeout(1300L) {
repeat(1000) { i ->
println("I'm sleeping $i ...")
delay(500L)
}
}
+//sampleEnd
}
```
@@ -314,10 +336,13 @@
you need to do some additional action specifically on any kind of timeout or use [withTimeoutOrNull] function
that is similar to [withTimeout], but returns `null` on timeout instead of throwing an exception:
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking {
+import kotlinx.coroutines.*
+
+fun main() = runBlocking {
+//sampleStart
val result = withTimeoutOrNull(1300L) {
repeat(1000) { i ->
println("I'm sleeping $i ...")
@@ -326,6 +351,7 @@
"Done" // will get cancelled before it produces this result
}
println("Result is $result")
+//sampleEnd
}
```
diff --git a/docs/channels.md b/docs/channels.md
index 9e88ce7..57e6c6d 100644
--- a/docs/channels.md
+++ b/docs/channels.md
@@ -5,9 +5,6 @@
// This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit.
package kotlinx.coroutines.guide.$$1$$2
-
-import kotlinx.coroutines.*
-import kotlinx.coroutines.channels.*
-->
<!--- KNIT ../core/kotlinx-coroutines-core/test/guide/.*\.kt -->
<!--- TEST_OUT ../core/kotlinx-coroutines-core/test/guide/test/ChannelsGuideTest.kt
@@ -36,10 +33,6 @@
<!--- END_TOC -->
-
-
-
-
## Channels (experimental)
Deferred values provide a convenient way to transfer a single value between coroutines.
@@ -56,10 +49,14 @@
a blocking `take` operation it has a suspending [receive][ReceiveChannel.receive].
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking {
+import kotlinx.coroutines.*
+import kotlinx.coroutines.channels.*
+
+fun main() = runBlocking {
+//sampleStart
val channel = Channel<Int>()
launch {
// this might be heavy CPU-consuming computation or async logic, we'll just send five squares
@@ -68,6 +65,7 @@
// here we print five received integers:
repeat(5) { println(channel.receive()) }
println("Done!")
+//sampleEnd
}
```
@@ -98,10 +96,14 @@
The iteration stops as soon as this close token is received, so there is a guarantee
that all previously sent elements before the close are received:
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking {
+import kotlinx.coroutines.*
+import kotlinx.coroutines.channels.*
+
+fun main() = runBlocking {
+//sampleStart
val channel = Channel<Int>()
launch {
for (x in 1..5) channel.send(x * x)
@@ -110,6 +112,7 @@
// here we print received values using `for` loop (until the channel is closed)
for (y in channel) println(y)
println("Done!")
+//sampleEnd
}
```
@@ -136,17 +139,22 @@
There is a convenient coroutine builder named [produce] that makes it easy to do it right on producer side,
and an extension function [consumeEach], that replaces a `for` loop on the consumer side:
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
+import kotlinx.coroutines.*
+import kotlinx.coroutines.channels.*
+
fun CoroutineScope.produceSquares(): ReceiveChannel<Int> = produce {
for (x in 1..5) send(x * x)
}
-fun main(args: Array<String>) = runBlocking {
+fun main() = runBlocking {
+//sampleStart
val squares = produceSquares()
squares.consumeEach { println(it) }
println("Done!")
+//sampleEnd
}
```
@@ -193,15 +201,34 @@
The main code starts and connects the whole pipeline:
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<!--- CLEAR -->
+
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking {
+import kotlinx.coroutines.*
+import kotlinx.coroutines.channels.*
+
+fun main() = runBlocking {
+//sampleStart
val numbers = produceNumbers() // produces integers from 1 and on
val squares = square(numbers) // squares integers
for (i in 1..5) println(squares.receive()) // print first five
println("Done!") // we are done
coroutineContext.cancelChildren() // cancel children coroutines
+//sampleEnd
+}
+
+fun CoroutineScope.produceSquares(): ReceiveChannel<Int> = produce {
+ for (x in 1..5) send(x * x)
+}
+
+fun CoroutineScope.produceNumbers() = produce<Int> {
+ var x = 1
+ while (true) send(x++) // infinite stream of integers starting from 1
+}
+fun CoroutineScope.square(numbers: ReceiveChannel<Int>): ReceiveChannel<Int> = produce {
+ for (x in numbers) send(x * x)
}
```
@@ -226,10 +253,6 @@
Let's take pipelines to the extreme with an example that generates prime numbers using a pipeline
of coroutines. We start with an infinite sequence of numbers.
-
-<!--- INCLUDE
-import kotlin.coroutines.*
--->
<div class="sample" markdown="1" theme="idea" data-highlight-only>
@@ -270,10 +293,16 @@
extension function to cancel all the children coroutines after we have printed
the first ten prime numbers.
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<!--- CLEAR -->
+
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking {
+import kotlinx.coroutines.*
+import kotlinx.coroutines.channels.*
+
+fun main() = runBlocking {
+//sampleStart
var cur = numbersFrom(2)
for (i in 1..10) {
val prime = cur.receive()
@@ -281,6 +310,16 @@
cur = filter(cur, prime)
}
coroutineContext.cancelChildren() // cancel all children to let main finish
+//sampleEnd
+}
+
+fun CoroutineScope.numbersFrom(start: Int) = produce<Int> {
+ var x = start
+ while (true) send(x++) // infinite stream of integers from start
+}
+
+fun CoroutineScope.filter(numbers: ReceiveChannel<Int>, prime: Int) = produce<Int> {
+ for (x in numbers) if (x % prime != 0) send(x)
}
```
@@ -355,14 +394,35 @@
Now let us launch five processors and let them work for almost a second. See what happens:
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<!--- CLEAR -->
+
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking<Unit> {
+import kotlinx.coroutines.*
+import kotlinx.coroutines.channels.*
+
+fun main() = runBlocking<Unit> {
+//sampleStart
val producer = produceNumbers()
repeat(5) { launchProcessor(it, producer) }
delay(950)
producer.cancel() // cancel producer coroutine and thus kill them all
+//sampleEnd
+}
+
+fun CoroutineScope.produceNumbers() = produce<Int> {
+ var x = 1 // start from 1
+ while (true) {
+ send(x++) // produce next
+ delay(100) // wait 0.1s
+ }
+}
+
+fun CoroutineScope.launchProcessor(id: Int, channel: ReceiveChannel<Int>) = launch {
+ for (msg in channel) {
+ println("Processor #$id received $msg")
+ }
}
```
@@ -402,10 +462,6 @@
For example, let us have a channel of strings, and a suspending function that
repeatedly sends a specified string to this channel with a specified delay:
-<!--- INCLUDE
-import kotlin.coroutines.*
--->
-
<div class="sample" markdown="1" theme="idea" data-highlight-only>
```kotlin
@@ -422,10 +478,16 @@
Now, let us see what happens if we launch a couple of coroutines sending strings
(in this example we launch them in the context of the main thread as main coroutine's children):
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<!--- CLEAR -->
+
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking {
+import kotlinx.coroutines.*
+import kotlinx.coroutines.channels.*
+
+fun main() = runBlocking {
+//sampleStart
val channel = Channel<String>()
launch { sendString(channel, "foo", 200L) }
launch { sendString(channel, "BAR!", 500L) }
@@ -433,6 +495,14 @@
println(channel.receive())
}
coroutineContext.cancelChildren() // cancel all children to let main finish
+//sampleEnd
+}
+
+suspend fun sendString(channel: SendChannel<String>, s: String, time: Long) {
+ while (true) {
+ delay(time)
+ channel.send(s)
+ }
}
```
@@ -465,14 +535,15 @@
Take a look at the behavior of the following code:
-<!--- INCLUDE
-import kotlin.coroutines.*
--->
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking<Unit> {
+import kotlinx.coroutines.*
+import kotlinx.coroutines.channels.*
+
+fun main() = runBlocking<Unit> {
+//sampleStart
val channel = Channel<Int>(4) // create buffered channel
val sender = launch { // launch sender coroutine
repeat(10) {
@@ -483,6 +554,7 @@
// don't receive anything... just wait....
delay(1000)
sender.cancel() // cancel sender coroutine
+//sampleEnd
}
```
@@ -511,16 +583,17 @@
gets the element. In the following example two coroutines "ping" and "pong" are
receiving the "ball" object from the shared "table" channel.
-<!--- INCLUDE
-import kotlin.coroutines.*
--->
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
+import kotlinx.coroutines.*
+import kotlinx.coroutines.channels.*
+
+//sampleStart
data class Ball(var hits: Int)
-fun main(args: Array<String>) = runBlocking {
+fun main() = runBlocking {
val table = Channel<Ball>() // a shared table
launch { player("ping", table) }
launch { player("pong", table) }
@@ -537,6 +610,7 @@
table.send(ball) // send the ball back
}
}
+//sampleEnd
```
</div>
@@ -574,7 +648,10 @@
<div class="sample" markdown="1" theme="idea" data-highlight-only>
```kotlin
-fun main(args: Array<String>) = runBlocking<Unit> {
+import kotlinx.coroutines.*
+import kotlinx.coroutines.channels.*
+
+fun main() = runBlocking<Unit> {
val tickerChannel = ticker(delayMillis = 100, initialDelayMillis = 0) // create ticker channel
var nextElement = withTimeoutOrNull(1) { tickerChannel.receive() }
println("Initial element is available immediately: $nextElement") // initial delay hasn't passed yet
diff --git a/docs/composing-suspending-functions.md b/docs/composing-suspending-functions.md
index bd9ccc9..ca34fdf 100644
--- a/docs/composing-suspending-functions.md
+++ b/docs/composing-suspending-functions.md
@@ -5,8 +5,6 @@
// This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit.
package kotlinx.coroutines.guide.$$1$$2
-
-import kotlinx.coroutines.*
-->
<!--- KNIT ../core/kotlinx-coroutines-core/test/guide/.*\.kt -->
<!--- TEST_OUT ../core/kotlinx-coroutines-core/test/guide/test/ComposingGuideTest.kt
@@ -41,10 +39,6 @@
remote service call or computation. We just pretend they are useful, but actually each one just
delays for a second for the purpose of this example:
-<!--- INCLUDE .*/example-compose-([0-9]+).kt
-import kotlin.system.*
--->
-
<div class="sample" markdown="1" theme="idea" data-highlight-only>
```kotlin
@@ -61,7 +55,6 @@
</div>
-<!--- INCLUDE .*/example-compose-([0-9]+).kt -->
What do we do if need to invoke them _sequentially_ -- first `doSomethingUsefulOne` _and then_
`doSomethingUsefulTwo` and compute the sum of their results?
@@ -72,16 +65,33 @@
code, is _sequential_ by default. The following example demonstrates it by measuring the total
time it takes to execute both suspending functions:
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<!--- CLEAR -->
+
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking<Unit> {
+import kotlinx.coroutines.*
+import kotlin.system.*
+
+fun main() = runBlocking<Unit> {
+//sampleStart
val time = measureTimeMillis {
val one = doSomethingUsefulOne()
val two = doSomethingUsefulTwo()
println("The answer is ${one + two}")
}
println("Completed in $time ms")
+//sampleEnd
+}
+
+suspend fun doSomethingUsefulOne(): Int {
+ delay(1000L) // pretend we are doing something useful here
+ return 13
+}
+
+suspend fun doSomethingUsefulTwo(): Int {
+ delay(1000L) // pretend we are doing something useful here, too
+ return 29
}
```
@@ -110,16 +120,31 @@
but `Deferred` is also a `Job`, so you can cancel it if needed.
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking<Unit> {
+import kotlinx.coroutines.*
+import kotlin.system.*
+
+fun main() = runBlocking<Unit> {
+//sampleStart
val time = measureTimeMillis {
val one = async { doSomethingUsefulOne() }
val two = async { doSomethingUsefulTwo() }
println("The answer is ${one.await() + two.await()}")
}
println("Completed in $time ms")
+//sampleEnd
+}
+
+suspend fun doSomethingUsefulOne(): Int {
+ delay(1000L) // pretend we are doing something useful here
+ return 13
+}
+
+suspend fun doSomethingUsefulTwo(): Int {
+ delay(1000L) // pretend we are doing something useful here, too
+ return 29
}
```
@@ -146,10 +171,14 @@
[await][Deferred.await] or if a [start][Job.start] function
is invoked. Run the following example:
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking<Unit> {
+import kotlinx.coroutines.*
+import kotlin.system.*
+
+fun main() = runBlocking<Unit> {
+//sampleStart
val time = measureTimeMillis {
val one = async(start = CoroutineStart.LAZY) { doSomethingUsefulOne() }
val two = async(start = CoroutineStart.LAZY) { doSomethingUsefulTwo() }
@@ -159,6 +188,17 @@
println("The answer is ${one.await() + two.await()}")
}
println("Completed in $time ms")
+//sampleEnd
+}
+
+suspend fun doSomethingUsefulOne(): Int {
+ delay(1000L) // pretend we are doing something useful here
+ return 13
+}
+
+suspend fun doSomethingUsefulTwo(): Int {
+ delay(1000L) // pretend we are doing something useful here, too
+ return 29
}
```
@@ -215,11 +255,17 @@
The following example shows their use outside of coroutine:
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<!--- CLEAR -->
+
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
+import kotlinx.coroutines.*
+import kotlin.system.*
+
+//sampleStart
// note, that we don't have `runBlocking` to the right of `main` in this example
-fun main(args: Array<String>) {
+fun main() {
val time = measureTimeMillis {
// we can initiate async actions outside of a coroutine
val one = somethingUsefulOneAsync()
@@ -232,6 +278,25 @@
}
println("Completed in $time ms")
}
+//sampleEnd
+
+fun somethingUsefulOneAsync() = GlobalScope.async {
+ doSomethingUsefulOne()
+}
+
+fun somethingUsefulTwoAsync() = GlobalScope.async {
+ doSomethingUsefulTwo()
+}
+
+suspend fun doSomethingUsefulOne(): Int {
+ delay(1000L) // pretend we are doing something useful here
+ return 13
+}
+
+suspend fun doSomethingUsefulTwo(): Int {
+ delay(1000L) // pretend we are doing something useful here, too
+ return 29
+}
```
</div>
@@ -276,14 +341,37 @@
This way, if something goes wrong inside the code of `concurrentSum` function and it throws an exception,
all the coroutines that were launched in its scope are cancelled.
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<!--- CLEAR -->
+
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking<Unit> {
+import kotlinx.coroutines.*
+import kotlin.system.*
+
+fun main() = runBlocking<Unit> {
+//sampleStart
val time = measureTimeMillis {
println("The answer is ${concurrentSum()}")
}
println("Completed in $time ms")
+//sampleEnd
+}
+
+suspend fun concurrentSum(): Int = coroutineScope {
+ val one = async { doSomethingUsefulOne() }
+ val two = async { doSomethingUsefulTwo() }
+ one.await() + two.await()
+}
+
+suspend fun doSomethingUsefulOne(): Int {
+ delay(1000L) // pretend we are doing something useful here
+ return 13
+}
+
+suspend fun doSomethingUsefulTwo(): Int {
+ delay(1000L) // pretend we are doing something useful here, too
+ return 29
}
```
@@ -302,10 +390,14 @@
Cancellation is always propagated through coroutines hierarchy:
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<!--- CLEAR -->
+
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking<Unit> {
+import kotlinx.coroutines.*
+
+fun main() = runBlocking<Unit> {
try {
failedConcurrentSum()
} catch(e: ArithmeticException) {
diff --git a/docs/coroutine-context-and-dispatchers.md b/docs/coroutine-context-and-dispatchers.md
index a89b13d..bcbe657 100644
--- a/docs/coroutine-context-and-dispatchers.md
+++ b/docs/coroutine-context-and-dispatchers.md
@@ -5,8 +5,6 @@
// This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit.
package kotlinx.coroutines.guide.$$1$$2
-
-import kotlinx.coroutines.*
-->
<!--- KNIT ../core/kotlinx-coroutines-core/test/guide/.*\.kt -->
<!--- TEST_OUT ../core/kotlinx-coroutines-core/test/guide/test/DispatcherGuideTest.kt
@@ -58,14 +56,13 @@
Try the following example:
-<!--- INCLUDE
-import kotlin.coroutines.*
--->
-
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking<Unit> {
+import kotlinx.coroutines.*
+
+fun main() = runBlocking<Unit> {
+//sampleStart
launch { // context of the parent, main runBlocking coroutine
println("main runBlocking : I'm working in thread ${Thread.currentThread().name}")
}
@@ -78,6 +75,7 @@
launch(newSingleThreadContext("MyOwnThread")) { // will get its own new thread
println("newSingleThreadContext: I'm working in thread ${Thread.currentThread().name}")
}
+//sampleEnd
}
```
@@ -124,14 +122,13 @@
is confined to the invoker thread, so inheriting it has the effect of confining execution to
this thread with a predictable FIFO scheduling.
-<!--- INCLUDE
-import kotlin.coroutines.*
--->
-
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking<Unit> {
+import kotlinx.coroutines.*
+
+fun main() = runBlocking<Unit> {
+//sampleStart
launch(Dispatchers.Unconfined) { // not confined -- will work with main thread
println("Unconfined : I'm working in thread ${Thread.currentThread().name}")
delay(500)
@@ -142,6 +139,7 @@
delay(1000)
println("main runBlocking: After delay in thread ${Thread.currentThread().name}")
}
+//sampleEnd
}
```
@@ -180,16 +178,15 @@
Run the following code with `-Dkotlinx.coroutines.debug` JVM option:
-<!--- INCLUDE
-import kotlin.coroutines.*
--->
-
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
+import kotlinx.coroutines.*
+
fun log(msg: String) = println("[${Thread.currentThread().name}] $msg")
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
+//sampleStart
val a = async {
log("I'm computing a piece of the answer")
6
@@ -199,6 +196,7 @@
7
}
log("The answer is ${a.await() * b.await()}")
+//sampleEnd
}
```
@@ -229,12 +227,15 @@
Run the following code with `-Dkotlinx.coroutines.debug` JVM option (see [debug](#debugging-coroutines-and-threads)):
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
+import kotlinx.coroutines.*
+
fun log(msg: String) = println("[${Thread.currentThread().name}] $msg")
-fun main(args: Array<String>) {
+fun main() {
+//sampleStart
newSingleThreadContext("Ctx1").use { ctx1 ->
newSingleThreadContext("Ctx2").use { ctx2 ->
runBlocking(ctx1) {
@@ -246,6 +247,7 @@
}
}
}
+//sampleEnd
}
```
@@ -273,15 +275,15 @@
The coroutine's [Job] is part of its context. The coroutine can retrieve it from its own context
using `coroutineContext[Job]` expression:
-<!--- INCLUDE
-import kotlin.coroutines.*
--->
-
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking<Unit> {
+import kotlinx.coroutines.*
+
+fun main() = runBlocking<Unit> {
+//sampleStart
println("My job is ${coroutineContext[Job]}")
+//sampleEnd
}
```
@@ -311,14 +313,14 @@
However, when [GlobalScope] is used to launch a coroutine, it is not tied to the scope it
was launched from and operates independently.
-<!--- INCLUDE
-import kotlin.coroutines.*
--->
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking<Unit> {
+import kotlinx.coroutines.*
+
+fun main() = runBlocking<Unit> {
+//sampleStart
// launch a coroutine to process some kind of incoming request
val request = launch {
// it spawns two other jobs, one with GlobalScope
@@ -339,6 +341,7 @@
request.cancel() // cancel processing of the request
delay(1000) // delay a second to see what happens
println("main: Who has survived request cancellation?")
+//sampleEnd
}
```
@@ -362,14 +365,13 @@
A parent coroutine always waits for completion of all its children. Parent does not have to explicitly track
all the children it launches and it does not have to use [Job.join] to wait for them at the end:
-<!--- INCLUDE
-import kotlin.coroutines.*
--->
-
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking<Unit> {
+import kotlinx.coroutines.*
+
+fun main() = runBlocking<Unit> {
+//sampleStart
// launch a coroutine to process some kind of incoming request
val request = launch {
repeat(3) { i -> // launch a few children jobs
@@ -382,6 +384,7 @@
}
request.join() // wait for completion of the request, including all its children
println("Now processing of the request is complete")
+//sampleEnd
}
```
@@ -411,12 +414,15 @@
The following example demonstrates this concept:
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
+import kotlinx.coroutines.*
+
fun log(msg: String) = println("[${Thread.currentThread().name}] $msg")
-fun main(args: Array<String>) = runBlocking(CoroutineName("main")) {
+fun main() = runBlocking(CoroutineName("main")) {
+//sampleStart
log("Started main coroutine")
// run two background value computations
val v1 = async(CoroutineName("v1coroutine")) {
@@ -430,6 +436,7 @@
6
}
log("The answer for v1 / v2 = ${v1.await() / v2.await()}")
+//sampleEnd
}
```
@@ -454,17 +461,17 @@
For example, we can launch a coroutine with an explicitly specified dispatcher and an explicitly specified
name at the same time:
-<!--- INCLUDE
-import kotlin.coroutines.*
--->
-
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking<Unit> {
+import kotlinx.coroutines.*
+
+fun main() = runBlocking<Unit> {
+//sampleStart
launch(Dispatchers.Default + CoroutineName("test")) {
println("I'm working in thread ${Thread.currentThread().name}")
}
+//sampleEnd
}
```
@@ -492,9 +499,6 @@
the lifecycle of our activity. A job instance is created using [Job()] factory function when
activity is created and it is cancelled when an activity is destroyed like this:
-<!--- INCLUDE
-import kotlin.coroutines.*
--->
<div class="sample" markdown="1" theme="idea" data-highlight-only>
@@ -554,10 +558,45 @@
This cancels all the coroutines that were launched which we can confirm by noting that it does not print
onto the screen anymore if we wait:
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<!--- CLEAR -->
+
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking<Unit> {
+import kotlin.coroutines.*
+import kotlinx.coroutines.*
+
+class Activity : CoroutineScope {
+ lateinit var job: Job
+
+ fun create() {
+ job = Job()
+ }
+
+ fun destroy() {
+ job.cancel()
+ }
+ // to be continued ...
+
+ // class Activity continues
+ override val coroutineContext: CoroutineContext
+ get() = Dispatchers.Default + job
+ // to be continued ...
+
+ // class Activity continues
+ fun doSomething() {
+ // launch ten coroutines for a demo, each working for a different time
+ repeat(10) { i ->
+ launch {
+ delay((i + 1) * 200L) // variable delay 200ms, 400ms, ... etc
+ println("Coroutine $i is done")
+ }
+ }
+ }
+} // class Activity ends
+
+fun main() = runBlocking<Unit> {
+//sampleStart
val activity = Activity()
activity.create() // create an activity
activity.doSomething() // run test function
@@ -566,6 +605,7 @@
println("Destroying activity!")
activity.destroy() // cancels all coroutines
delay(1000) // visually confirm that they don't work
+//sampleEnd
}
```
@@ -598,16 +638,15 @@
It is easy to demonstrate it in action:
-<!--- INCLUDE
-import kotlin.coroutines.*
--->
-
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
+import kotlinx.coroutines.*
+
val threadLocal = ThreadLocal<String?>() // declare thread-local variable
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
+//sampleStart
threadLocal.set("main")
println("Pre-main, current thread: ${Thread.currentThread()}, thread local value: '${threadLocal.get()}'")
val job = launch(Dispatchers.Default + threadLocal.asContextElement(value = "launch")) {
@@ -617,6 +656,7 @@
}
job.join()
println("Post-main, current thread: ${Thread.currentThread()}, thread local value: '${threadLocal.get()}'")
+//sampleEnd
}
```
diff --git a/docs/exception-handling.md b/docs/exception-handling.md
index d50fba0..9e5334d 100644
--- a/docs/exception-handling.md
+++ b/docs/exception-handling.md
@@ -5,8 +5,6 @@
// This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit.
package kotlinx.coroutines.guide.$$1$$2
-
-import kotlinx.coroutines.*
-->
<!--- KNIT ../core/kotlinx-coroutines-core/test/guide/.*\.kt -->
<!--- TEST_OUT ../core/kotlinx-coroutines-core/test/guide/test/ExceptionsGuideTest.kt
@@ -35,8 +33,6 @@
## Exception handling
-<!--- INCLUDE .*/example-exceptions-([0-9]+).kt
--->
This section covers exception handling and cancellation on exceptions.
We already know that cancelled coroutine throws [CancellationException] in suspension points and that it
@@ -57,7 +53,9 @@
<div class="sample" markdown="1" theme="idea" data-highlight-only>
```kotlin
-fun main(args: Array<String>) = runBlocking {
+import kotlinx.coroutines.*
+
+fun main() = runBlocking {
val job = GlobalScope.launch {
println("Throwing exception from launch")
throw IndexOutOfBoundsException() // Will be printed to the console by Thread.defaultUncaughtExceptionHandler
@@ -109,10 +107,13 @@
[CoroutineExceptionHandler] is invoked only on exceptions which are not expected to be handled by the user,
so registering it in [async] builder and the like of it has no effect.
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking {
+import kotlinx.coroutines.*
+
+fun main() = runBlocking {
+//sampleStart
val handler = CoroutineExceptionHandler { _, exception ->
println("Caught $exception")
}
@@ -123,6 +124,7 @@
throw ArithmeticException() // Nothing will be printed, relying on user to call deferred.await()
}
joinAll(job, deferred)
+//sampleEnd
}
```
@@ -146,14 +148,13 @@
When a coroutine is cancelled using [Job.cancel] without a cause, it terminates, but it does not cancel its parent.
Cancelling without cause is a mechanism for parent to cancel its children without cancelling itself.
-<!--- INCLUDE
-import kotlin.coroutines.*
--->
-
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking {
+import kotlinx.coroutines.*
+
+fun main() = runBlocking {
+//sampleStart
val job = launch {
val child = launch {
try {
@@ -170,6 +171,7 @@
println("Parent is not cancelled")
}
job.join()
+//sampleEnd
}
```
@@ -198,14 +200,13 @@
is launched in the scope of the main [runBlocking], since the main coroutine is going to be always cancelled
when its child completes with exception despite the installed handler.
-<!--- INCLUDE
-import kotlin.coroutines.*
--->
-
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking {
+import kotlinx.coroutines.*
+
+fun main() = runBlocking {
+//sampleEnd
val handler = CoroutineExceptionHandler { _, exception ->
println("Caught $exception")
}
@@ -228,6 +229,7 @@
}
}
job.join()
+//sampleEnd
}
```
@@ -257,16 +259,19 @@
would cause implementation details of a coroutines (whether it had delegated parts of its work to its children or not)
to leak to its exception handler.
+
<!--- INCLUDE
+
import kotlinx.coroutines.exceptions.*
-import kotlin.coroutines.*
-import java.io.*
-->
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking {
+import kotlinx.coroutines.*
+import java.io.*
+
+fun main() = runBlocking {
val handler = CoroutineExceptionHandler { _, exception ->
println("Caught $exception with suppressed ${exception.suppressed.contentToString()}")
}
@@ -284,7 +289,7 @@
}
delay(Long.MAX_VALUE)
}
- job.join()
+ job.join()
}
```
@@ -307,15 +312,14 @@
Cancellation exceptions are transparent and unwrapped by default:
-<!--- INCLUDE
-import kotlin.coroutines.*
-import java.io.*
--->
-
-<div class="sample" markdown="1" theme="idea" data-highlight-only>
+<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
```kotlin
-fun main(args: Array<String>) = runBlocking {
+import kotlinx.coroutines.*
+import java.io.*
+
+fun main() = runBlocking {
+//sampleStart
val handler = CoroutineExceptionHandler { _, exception ->
println("Caught original $exception")
}
@@ -335,6 +339,7 @@
}
}
job.join()
+//sampleEnd
}
```
@@ -367,14 +372,12 @@
For these purposes [SupervisorJob][SupervisorJob()] can be used. It is similar to a regular [Job][Job()] with the only exception that cancellation is propagated
only downwards. It is easy to demonstrate with an example:
-<!--- INCLUDE
-import kotlin.coroutines.*
--->
-
<div class="sample" markdown="1" theme="idea" data-highlight-only>
```kotlin
-fun main(args: Array<String>) = runBlocking {
+import kotlinx.coroutines.*
+
+fun main() = runBlocking {
val supervisor = SupervisorJob()
with(CoroutineScope(coroutineContext + supervisor)) {
// launch the first child -- its exception is ignored for this example (don't do this in practice!)
@@ -424,14 +427,13 @@
only in one direction and cancels all children only if it has failed itself. It also waits for all children before completion
just like [coroutineScope] does.
-<!--- INCLUDE
-import kotlin.coroutines.*
--->
-
<div class="sample" markdown="1" theme="idea" data-highlight-only>
```kotlin
-fun main(args: Array<String>) = runBlocking {
+import kotlin.coroutines.*
+import kotlinx.coroutines.*
+
+fun main() = runBlocking {
try {
supervisorScope {
val child = launch {
@@ -473,14 +475,13 @@
Every child should handle its exceptions by itself via exception handling mechanisms.
This difference comes from the fact that child's failure is not propagated to the parent.
-<!--- INCLUDE
-import kotlin.coroutines.*
--->
-
<div class="sample" markdown="1" theme="idea" data-highlight-only>
```kotlin
-fun main(args: Array<String>) = runBlocking {
+import kotlin.coroutines.*
+import kotlinx.coroutines.*
+
+fun main() = runBlocking {
val handler = CoroutineExceptionHandler { _, exception ->
println("Caught $exception")
}
diff --git a/docs/select-expression.md b/docs/select-expression.md
index 55437f9..842ab3a 100644
--- a/docs/select-expression.md
+++ b/docs/select-expression.md
@@ -108,7 +108,7 @@
<div class="sample" markdown="1" theme="idea" data-highlight-only>
```kotlin
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
val fizz = fizz()
val buzz = buzz()
repeat(7) {
@@ -175,7 +175,7 @@
<div class="sample" markdown="1" theme="idea" data-highlight-only>
```kotlin
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
val a = produce<String> {
repeat(4) { send("Hello $it") }
}
@@ -251,7 +251,7 @@
<div class="sample" markdown="1" theme="idea" data-highlight-only>
```kotlin
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
val side = Channel<Int>() // allocate side channel
launch { // this is a very fast consumer for the side channel
side.consumeEach { println("Side channel has $it") }
@@ -329,7 +329,7 @@
<div class="sample" markdown="1" theme="idea" data-highlight-only>
```kotlin
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
val list = asyncStringsList()
val result = select<String> {
list.withIndex().forEach { (index, deferred) ->
@@ -414,7 +414,7 @@
<div class="sample" markdown="1" theme="idea" data-highlight-only>
```kotlin
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
val chan = Channel<Deferred<String>>() // the channel for test
launch { // launch printing coroutine
for (s in switchMapDeferreds(chan))
diff --git a/docs/shared-mutable-state-and-concurrency.md b/docs/shared-mutable-state-and-concurrency.md
index 55808a4..0ddbdab 100644
--- a/docs/shared-mutable-state-and-concurrency.md
+++ b/docs/shared-mutable-state-and-concurrency.md
@@ -91,7 +91,7 @@
```kotlin
var counter = 0
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
GlobalScope.massiveRun {
counter++
}
@@ -121,7 +121,7 @@
val mtContext = newFixedThreadPoolContext(2, "mtPool") // explicitly define context with two threads
var counter = 0
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
CoroutineScope(mtContext).massiveRun { // use it instead of Dispatchers.Default in this sample and below
counter++
}
@@ -148,7 +148,7 @@
@Volatile // in Kotlin `volatile` is an annotation
var counter = 0
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
GlobalScope.massiveRun {
counter++
}
@@ -181,7 +181,7 @@
```kotlin
var counter = AtomicInteger()
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
GlobalScope.massiveRun {
counter.incrementAndGet()
}
@@ -215,7 +215,7 @@
val counterContext = newSingleThreadContext("CounterContext")
var counter = 0
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
GlobalScope.massiveRun { // run each coroutine with DefaultDispathcer
withContext(counterContext) { // but confine each increment to the single-threaded context
counter++
@@ -250,7 +250,7 @@
val counterContext = newSingleThreadContext("CounterContext")
var counter = 0
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
CoroutineScope(counterContext).massiveRun { // run each coroutine in the single-threaded context
counter++
}
@@ -285,7 +285,7 @@
val mutex = Mutex()
var counter = 0
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
GlobalScope.massiveRun {
mutex.withLock {
counter++
@@ -360,7 +360,7 @@
<div class="sample" markdown="1" theme="idea" data-highlight-only>
```kotlin
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
val counter = counterActor() // create the actor
GlobalScope.massiveRun {
counter.send(IncCounter)
diff --git a/knit/src/Knit.kt b/knit/src/Knit.kt
index 71451f0..e212206 100644
--- a/knit/src/Knit.kt
+++ b/knit/src/Knit.kt
@@ -279,7 +279,7 @@
testOutLines += ""
testOutLines += " @Test"
testOutLines += " fun test$funName() {"
- val prefix = " test(\"$funName\") { $pgk.main(emptyArray()) }"
+ val prefix = " test(\"$funName\") { $pgk.main() }"
when (predicate) {
"" -> makeTestLines(testOutLines, prefix, "verifyLines", test)
STARTS_WITH_PREDICATE -> makeTestLines(testOutLines, prefix, "verifyLinesStartWith", test)
diff --git a/reactive/coroutines-guide-reactive.md b/reactive/coroutines-guide-reactive.md
index bf6399e..8935d4a 100644
--- a/reactive/coroutines-guide-reactive.md
+++ b/reactive/coroutines-guide-reactive.md
@@ -93,7 +93,7 @@
-->
```kotlin
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
// create a channel that produces numbers from 1 to 3 with 200ms delays between them
val source = produce<Int> {
println("Begin") // mark the beginning of this coroutine in output
@@ -150,7 +150,7 @@
-->
```kotlin
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
// create a publisher that produces numbers from 1 to 3 with 200ms delays between them
val source = publish<Int> {
// ^^^^^^^ <--- Difference from the previous examples is here
@@ -230,7 +230,7 @@
-->
```kotlin
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
val source = Flowable.range(1, 5) // a range of five numbers
.doOnSubscribe { println("OnSubscribe") } // provide some insight
.doOnComplete { println("OnComplete") } // ...
@@ -279,7 +279,7 @@
-->
```kotlin
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
val source = Flowable.range(1, 5) // a range of five numbers
.doOnSubscribe { println("OnSubscribe") } // provide some insight
.doOnComplete { println("OnComplete") } // ...
@@ -340,7 +340,7 @@
-->
```kotlin
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
// coroutine -- fast producer of elements in the context of the main thread
val source = rxFlowable {
for (x in 1..3) {
@@ -393,7 +393,7 @@
-->
```kotlin
-fun main(args: Array<String>) {
+fun main() {
val subject = BehaviorSubject.create<String>()
subject.onNext("one")
subject.onNext("two") // updates the state of BehaviorSubject, "one" value is lost
@@ -426,7 +426,7 @@
-->
```kotlin
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
val subject = BehaviorSubject.create<String>()
subject.onNext("one")
subject.onNext("two")
@@ -470,7 +470,7 @@
-->
```kotlin
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
val subject = BehaviorSubject.create<String>()
subject.onNext("one")
subject.onNext("two")
@@ -506,7 +506,7 @@
-->
```kotlin
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
val broadcast = ConflatedBroadcastChannel<String>()
broadcast.offer("one")
broadcast.offer("two")
@@ -582,7 +582,7 @@
It is straightforward to use from a coroutine:
```kotlin
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
// Range inherits parent job from runBlocking, but overrides dispatcher with Dispatchers.Default
range(Dispatchers.Default, 1, 5).consumeEach { println(it) }
}
@@ -641,7 +641,7 @@
-->
```kotlin
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
range(1, 5)
.fusedFilterMap(coroutineContext, { it % 2 == 0}, { "$it is even" })
.consumeEach { println(it) } // print all the resulting strings
@@ -714,7 +714,7 @@
The following code shows how `takeUntil` works:
```kotlin
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
val slowNums = rangeWithInterval(200, 1, 10) // numbers with 200ms interval
val stop = rangeWithInterval(500, 1, 10) // the first one after 500ms
slowNums.takeUntil(coroutineContext, stop).consumeEach { println(it) } // let's test it
@@ -791,7 +791,7 @@
The test code is to use `merge` on `testPub` and to display the results:
```kotlin
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
testPub().merge(coroutineContext).consumeEach { println(it) } // print the whole stream
}
```
@@ -839,7 +839,7 @@
Flowable.interval(time, TimeUnit.MILLISECONDS, scheduler),
BiFunction { x, _ -> x })
-fun main(args: Array<String>) {
+fun main() {
rangeWithIntervalRx(Schedulers.computation(), 100, 1, 3)
.subscribe { println("$it on thread ${Thread.currentThread().name}") }
Thread.sleep(1000)
@@ -881,7 +881,7 @@
}
}
-fun main(args: Array<String>) {
+fun main() {
Flowable.fromPublisher(rangeWithInterval(Dispatchers.Default, 100, 1, 3))
.subscribe { println("$it on thread ${Thread.currentThread().name}") }
Thread.sleep(1000)
@@ -931,7 +931,7 @@
}
}
-fun main(args: Array<String>) {
+fun main() {
Flowable.fromPublisher(rangeWithInterval(Dispatchers.Default, 100, 1, 3))
.observeOn(Schedulers.computation()) // <-- THIS LINE IS ADDED
.subscribe { println("$it on thread ${Thread.currentThread().name}") }
@@ -973,7 +973,7 @@
Flowable.interval(time, TimeUnit.MILLISECONDS, scheduler),
BiFunction { x, _ -> x })
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
rangeWithIntervalRx(Schedulers.computation(), 100, 1, 3)
.consumeEach { println("$it on thread ${Thread.currentThread().name}") }
}
@@ -1018,7 +1018,7 @@
Flowable.interval(time, TimeUnit.MILLISECONDS, scheduler),
BiFunction { x, _ -> x })
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
val job = launch(Dispatchers.Unconfined) { // launch new coroutine in Unconfined context (without its own thread pool)
rangeWithIntervalRx(Schedulers.computation(), 100, 1, 3)
.consumeEach { println("$it on thread ${Thread.currentThread().name}") }
diff --git a/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-basic-01.kt b/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-basic-01.kt
index 235c37b..19c67f8 100644
--- a/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-basic-01.kt
+++ b/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-basic-01.kt
@@ -9,7 +9,7 @@
import kotlinx.coroutines.channels.*
import kotlin.coroutines.*
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
// create a channel that produces numbers from 1 to 3 with 200ms delays between them
val source = produce<Int> {
println("Begin") // mark the beginning of this coroutine in output
diff --git a/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-basic-02.kt b/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-basic-02.kt
index f16bd87..0bb8431 100644
--- a/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-basic-02.kt
+++ b/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-basic-02.kt
@@ -9,7 +9,7 @@
import kotlinx.coroutines.reactive.*
import kotlin.coroutines.*
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
// create a publisher that produces numbers from 1 to 3 with 200ms delays between them
val source = publish<Int> {
// ^^^^^^^ <--- Difference from the previous examples is here
diff --git a/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-basic-03.kt b/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-basic-03.kt
index 7a77891..5846991 100644
--- a/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-basic-03.kt
+++ b/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-basic-03.kt
@@ -10,7 +10,7 @@
import kotlinx.coroutines.channels.*
import kotlinx.coroutines.reactive.*
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
val source = Flowable.range(1, 5) // a range of five numbers
.doOnSubscribe { println("OnSubscribe") } // provide some insight
.doOnComplete { println("OnComplete") } // ...
diff --git a/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-basic-04.kt b/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-basic-04.kt
index 22031c7..bbfb169 100644
--- a/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-basic-04.kt
+++ b/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-basic-04.kt
@@ -10,7 +10,7 @@
import kotlinx.coroutines.reactive.*
import kotlin.coroutines.*
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
val source = Flowable.range(1, 5) // a range of five numbers
.doOnSubscribe { println("OnSubscribe") } // provide some insight
.doOnComplete { println("OnComplete") } // ...
diff --git a/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-basic-05.kt b/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-basic-05.kt
index 94e5cfe..5fa322e 100644
--- a/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-basic-05.kt
+++ b/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-basic-05.kt
@@ -10,7 +10,7 @@
import kotlinx.coroutines.rx2.*
import kotlin.coroutines.*
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
// coroutine -- fast producer of elements in the context of the main thread
val source = rxFlowable {
for (x in 1..3) {
diff --git a/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-basic-06.kt b/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-basic-06.kt
index 704f7ca..6c730b3 100644
--- a/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-basic-06.kt
+++ b/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-basic-06.kt
@@ -7,7 +7,7 @@
import io.reactivex.subjects.BehaviorSubject
-fun main(args: Array<String>) {
+fun main() {
val subject = BehaviorSubject.create<String>()
subject.onNext("one")
subject.onNext("two") // updates the state of BehaviorSubject, "one" value is lost
diff --git a/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-basic-07.kt b/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-basic-07.kt
index 21abd23..74c45cb 100644
--- a/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-basic-07.kt
+++ b/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-basic-07.kt
@@ -9,7 +9,7 @@
import kotlinx.coroutines.*
import kotlinx.coroutines.rx2.consumeEach
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
val subject = BehaviorSubject.create<String>()
subject.onNext("one")
subject.onNext("two")
diff --git a/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-basic-08.kt b/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-basic-08.kt
index 1df294c..1eb82bc 100644
--- a/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-basic-08.kt
+++ b/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-basic-08.kt
@@ -10,7 +10,7 @@
import kotlinx.coroutines.rx2.*
import kotlin.coroutines.*
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
val subject = BehaviorSubject.create<String>()
subject.onNext("one")
subject.onNext("two")
diff --git a/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-basic-09.kt b/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-basic-09.kt
index 01ed149..d11e7e4 100644
--- a/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-basic-09.kt
+++ b/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-basic-09.kt
@@ -9,7 +9,7 @@
import kotlinx.coroutines.*
import kotlin.coroutines.*
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
val broadcast = ConflatedBroadcastChannel<String>()
broadcast.offer("one")
broadcast.offer("two")
diff --git a/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-context-01.kt b/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-context-01.kt
index a6bc49f..2b03fbe 100644
--- a/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-context-01.kt
+++ b/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-context-01.kt
@@ -16,7 +16,7 @@
Flowable.interval(time, TimeUnit.MILLISECONDS, scheduler),
BiFunction { x, _ -> x })
-fun main(args: Array<String>) {
+fun main() {
rangeWithIntervalRx(Schedulers.computation(), 100, 1, 3)
.subscribe { println("$it on thread ${Thread.currentThread().name}") }
Thread.sleep(1000)
diff --git a/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-context-02.kt b/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-context-02.kt
index b47a50b..9dea738 100644
--- a/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-context-02.kt
+++ b/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-context-02.kt
@@ -17,7 +17,7 @@
}
}
-fun main(args: Array<String>) {
+fun main() {
Flowable.fromPublisher(rangeWithInterval(Dispatchers.Default, 100, 1, 3))
.subscribe { println("$it on thread ${Thread.currentThread().name}") }
Thread.sleep(1000)
diff --git a/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-context-03.kt b/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-context-03.kt
index 0aacce1..35757be 100644
--- a/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-context-03.kt
+++ b/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-context-03.kt
@@ -18,7 +18,7 @@
}
}
-fun main(args: Array<String>) {
+fun main() {
Flowable.fromPublisher(rangeWithInterval(Dispatchers.Default, 100, 1, 3))
.observeOn(Schedulers.computation()) // <-- THIS LINE IS ADDED
.subscribe { println("$it on thread ${Thread.currentThread().name}") }
diff --git a/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-context-04.kt b/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-context-04.kt
index 00e8d31..0e66738 100644
--- a/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-context-04.kt
+++ b/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-context-04.kt
@@ -18,7 +18,7 @@
Flowable.interval(time, TimeUnit.MILLISECONDS, scheduler),
BiFunction { x, _ -> x })
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
rangeWithIntervalRx(Schedulers.computation(), 100, 1, 3)
.consumeEach { println("$it on thread ${Thread.currentThread().name}") }
}
diff --git a/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-context-05.kt b/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-context-05.kt
index 9ead166..9bf24f0 100644
--- a/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-context-05.kt
+++ b/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-context-05.kt
@@ -18,7 +18,7 @@
Flowable.interval(time, TimeUnit.MILLISECONDS, scheduler),
BiFunction { x, _ -> x })
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
val job = launch(Dispatchers.Unconfined) { // launch new coroutine in Unconfined context (without its own thread pool)
rangeWithIntervalRx(Schedulers.computation(), 100, 1, 3)
.consumeEach { println("$it on thread ${Thread.currentThread().name}") }
diff --git a/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-operators-01.kt b/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-operators-01.kt
index 8cab259..8d52cbb 100644
--- a/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-operators-01.kt
+++ b/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-operators-01.kt
@@ -13,7 +13,7 @@
for (x in start until start + count) send(x)
}
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
// Range inherits parent job from runBlocking, but overrides dispatcher with Dispatchers.Default
range(Dispatchers.Default, 1, 5).consumeEach { println(it) }
}
diff --git a/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-operators-02.kt b/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-operators-02.kt
index eac5100..d1ce00c 100644
--- a/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-operators-02.kt
+++ b/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-operators-02.kt
@@ -25,7 +25,7 @@
for (x in start until start + count) send(x)
}
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
range(1, 5)
.fusedFilterMap(coroutineContext, { it % 2 == 0}, { "$it is even" })
.consumeEach { println(it) } // print all the resulting strings
diff --git a/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-operators-03.kt b/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-operators-03.kt
index 196f3fe..4c49326 100644
--- a/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-operators-03.kt
+++ b/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-operators-03.kt
@@ -32,7 +32,7 @@
}
}
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
val slowNums = rangeWithInterval(200, 1, 10) // numbers with 200ms interval
val stop = rangeWithInterval(500, 1, 10) // the first one after 500ms
slowNums.takeUntil(coroutineContext, stop).consumeEach { println(it) } // let's test it
diff --git a/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-operators-04.kt b/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-operators-04.kt
index f1e0cc8..44d1557 100644
--- a/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-operators-04.kt
+++ b/reactive/kotlinx-coroutines-rx2/test/guide/example-reactive-operators-04.kt
@@ -32,6 +32,6 @@
delay(1100) // wait for 1.1s - done in 1.2 sec after start
}
-fun main(args: Array<String>) = runBlocking<Unit> {
+fun main() = runBlocking<Unit> {
testPub().merge(coroutineContext).consumeEach { println(it) } // print the whole stream
}
diff --git a/reactive/kotlinx-coroutines-rx2/test/guide/test/GuideReactiveTest.kt b/reactive/kotlinx-coroutines-rx2/test/guide/test/GuideReactiveTest.kt
index ea18468..65ac3e6 100644
--- a/reactive/kotlinx-coroutines-rx2/test/guide/test/GuideReactiveTest.kt
+++ b/reactive/kotlinx-coroutines-rx2/test/guide/test/GuideReactiveTest.kt
@@ -8,7 +8,7 @@
@Test
fun testKotlinxCoroutinesRx2GuideBasic01() {
- test("KotlinxCoroutinesRx2GuideBasic01") { kotlinx.coroutines.rx2.guide.basic01.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesRx2GuideBasic01") { kotlinx.coroutines.rx2.guide.basic01.main() }.verifyLines(
"Elements:",
"Begin",
"1",
@@ -20,7 +20,7 @@
@Test
fun testKotlinxCoroutinesRx2GuideBasic02() {
- test("KotlinxCoroutinesRx2GuideBasic02") { kotlinx.coroutines.rx2.guide.basic02.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesRx2GuideBasic02") { kotlinx.coroutines.rx2.guide.basic02.main() }.verifyLines(
"Elements:",
"Begin",
"1",
@@ -36,7 +36,7 @@
@Test
fun testKotlinxCoroutinesRx2GuideBasic03() {
- test("KotlinxCoroutinesRx2GuideBasic03") { kotlinx.coroutines.rx2.guide.basic03.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesRx2GuideBasic03") { kotlinx.coroutines.rx2.guide.basic03.main() }.verifyLines(
"OnSubscribe",
"1",
"2",
@@ -47,7 +47,7 @@
@Test
fun testKotlinxCoroutinesRx2GuideBasic04() {
- test("KotlinxCoroutinesRx2GuideBasic04") { kotlinx.coroutines.rx2.guide.basic04.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesRx2GuideBasic04") { kotlinx.coroutines.rx2.guide.basic04.main() }.verifyLines(
"OnSubscribe",
"1",
"2",
@@ -61,7 +61,7 @@
@Test
fun testKotlinxCoroutinesRx2GuideBasic05() {
- test("KotlinxCoroutinesRx2GuideBasic05") { kotlinx.coroutines.rx2.guide.basic05.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesRx2GuideBasic05") { kotlinx.coroutines.rx2.guide.basic05.main() }.verifyLines(
"Sent 1",
"Processed 1",
"Sent 2",
@@ -74,7 +74,7 @@
@Test
fun testKotlinxCoroutinesRx2GuideBasic06() {
- test("KotlinxCoroutinesRx2GuideBasic06") { kotlinx.coroutines.rx2.guide.basic06.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesRx2GuideBasic06") { kotlinx.coroutines.rx2.guide.basic06.main() }.verifyLines(
"two",
"three",
"four"
@@ -83,7 +83,7 @@
@Test
fun testKotlinxCoroutinesRx2GuideBasic07() {
- test("KotlinxCoroutinesRx2GuideBasic07") { kotlinx.coroutines.rx2.guide.basic07.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesRx2GuideBasic07") { kotlinx.coroutines.rx2.guide.basic07.main() }.verifyLines(
"two",
"three",
"four"
@@ -92,21 +92,21 @@
@Test
fun testKotlinxCoroutinesRx2GuideBasic08() {
- test("KotlinxCoroutinesRx2GuideBasic08") { kotlinx.coroutines.rx2.guide.basic08.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesRx2GuideBasic08") { kotlinx.coroutines.rx2.guide.basic08.main() }.verifyLines(
"four"
)
}
@Test
fun testKotlinxCoroutinesRx2GuideBasic09() {
- test("KotlinxCoroutinesRx2GuideBasic09") { kotlinx.coroutines.rx2.guide.basic09.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesRx2GuideBasic09") { kotlinx.coroutines.rx2.guide.basic09.main() }.verifyLines(
"four"
)
}
@Test
fun testKotlinxCoroutinesRx2GuideOperators01() {
- test("KotlinxCoroutinesRx2GuideOperators01") { kotlinx.coroutines.rx2.guide.operators01.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesRx2GuideOperators01") { kotlinx.coroutines.rx2.guide.operators01.main() }.verifyLines(
"1",
"2",
"3",
@@ -117,7 +117,7 @@
@Test
fun testKotlinxCoroutinesRx2GuideOperators02() {
- test("KotlinxCoroutinesRx2GuideOperators02") { kotlinx.coroutines.rx2.guide.operators02.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesRx2GuideOperators02") { kotlinx.coroutines.rx2.guide.operators02.main() }.verifyLines(
"2 is even",
"4 is even"
)
@@ -125,7 +125,7 @@
@Test
fun testKotlinxCoroutinesRx2GuideOperators03() {
- test("KotlinxCoroutinesRx2GuideOperators03") { kotlinx.coroutines.rx2.guide.operators03.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesRx2GuideOperators03") { kotlinx.coroutines.rx2.guide.operators03.main() }.verifyLines(
"1",
"2"
)
@@ -133,7 +133,7 @@
@Test
fun testKotlinxCoroutinesRx2GuideOperators04() {
- test("KotlinxCoroutinesRx2GuideOperators04") { kotlinx.coroutines.rx2.guide.operators04.main(emptyArray()) }.verifyLines(
+ test("KotlinxCoroutinesRx2GuideOperators04") { kotlinx.coroutines.rx2.guide.operators04.main() }.verifyLines(
"1",
"2",
"11",
@@ -146,7 +146,7 @@
@Test
fun testKotlinxCoroutinesRx2GuideContext01() {
- test("KotlinxCoroutinesRx2GuideContext01") { kotlinx.coroutines.rx2.guide.context01.main(emptyArray()) }.verifyLinesFlexibleThread(
+ test("KotlinxCoroutinesRx2GuideContext01") { kotlinx.coroutines.rx2.guide.context01.main() }.verifyLinesFlexibleThread(
"1 on thread RxComputationThreadPool-1",
"2 on thread RxComputationThreadPool-1",
"3 on thread RxComputationThreadPool-1"
@@ -155,7 +155,7 @@
@Test
fun testKotlinxCoroutinesRx2GuideContext02() {
- test("KotlinxCoroutinesRx2GuideContext02") { kotlinx.coroutines.rx2.guide.context02.main(emptyArray()) }.verifyLinesStart(
+ test("KotlinxCoroutinesRx2GuideContext02") { kotlinx.coroutines.rx2.guide.context02.main() }.verifyLinesStart(
"1 on thread ForkJoinPool.commonPool-worker-1",
"2 on thread ForkJoinPool.commonPool-worker-1",
"3 on thread ForkJoinPool.commonPool-worker-1"
@@ -164,7 +164,7 @@
@Test
fun testKotlinxCoroutinesRx2GuideContext03() {
- test("KotlinxCoroutinesRx2GuideContext03") { kotlinx.coroutines.rx2.guide.context03.main(emptyArray()) }.verifyLinesFlexibleThread(
+ test("KotlinxCoroutinesRx2GuideContext03") { kotlinx.coroutines.rx2.guide.context03.main() }.verifyLinesFlexibleThread(
"1 on thread RxComputationThreadPool-1",
"2 on thread RxComputationThreadPool-1",
"3 on thread RxComputationThreadPool-1"
@@ -173,7 +173,7 @@
@Test
fun testKotlinxCoroutinesRx2GuideContext04() {
- test("KotlinxCoroutinesRx2GuideContext04") { kotlinx.coroutines.rx2.guide.context04.main(emptyArray()) }.verifyLinesStart(
+ test("KotlinxCoroutinesRx2GuideContext04") { kotlinx.coroutines.rx2.guide.context04.main() }.verifyLinesStart(
"1 on thread main",
"2 on thread main",
"3 on thread main"
@@ -182,7 +182,7 @@
@Test
fun testKotlinxCoroutinesRx2GuideContext05() {
- test("KotlinxCoroutinesRx2GuideContext05") { kotlinx.coroutines.rx2.guide.context05.main(emptyArray()) }.verifyLinesStart(
+ test("KotlinxCoroutinesRx2GuideContext05") { kotlinx.coroutines.rx2.guide.context05.main() }.verifyLinesStart(
"1 on thread RxComputationThreadPool-1",
"2 on thread RxComputationThreadPool-1",
"3 on thread RxComputationThreadPool-1"