Skip to content

Commit f1c40fe

Browse files
committed
Fix code sample for log()
Run on java9(9.0.4) The log(msg: String) doesn't work like the description in document. `fun log(msg: String) = println("[${Thread.currentThread().name}] $msg")` The output is not same like expected in document: ``` [main] Unconfined [ForkJoinPool.commonPool-worker-2] Default [main] main runBlocking [MyOwnThread] newSingleThreadContext [kotlinx.coroutines.DefaultExecutor] Unconfined ``` I change to: `fun log(msg: String) = Thread.currentThread().run { println("[$name @coroutine#$id] $msg") }` output: ``` [main @coroutineKotlin#1] Unconfined [ForkJoinPool.commonPool-worker-2 @coroutineKotlin#15] Default [main @coroutineKotlin#1] main runBlocking [MyOwnThread @coroutineKotlin#17] newSingleThreadContext [kotlinx.coroutines.DefaultExecutor @coroutineKotlin#13] Unconfined ```
1 parent 52d2f93 commit f1c40fe

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

coroutines-guide.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,7 @@ import kotlin.coroutines.experimental.*
11291129
-->
11301130

11311131
```kotlin
1132-
fun log(msg: String) = println("[${Thread.currentThread().name}] $msg")
1132+
fun log(msg: String) = Thread.currentThread().run { println("[$name @coroutine#$id] $msg") }
11331133

11341134
fun main(args: Array<String>) = runBlocking<Unit> {
11351135
val a = async {
@@ -1170,7 +1170,7 @@ You can read more about debugging facilities in the documentation for [newCorout
11701170
Run the following code with `-Dkotlinx.coroutines.debug` JVM option (see [debug](#debugging-coroutines-and-threads)):
11711171

11721172
```kotlin
1173-
fun log(msg: String) = println("[${Thread.currentThread().name}] $msg")
1173+
fun log(msg: String) = Thread.currentThread().run { println("[$name @coroutine#$id] $msg") }
11741174

11751175
fun main(args: Array<String>) {
11761176
newSingleThreadContext("Ctx1").use { ctx1 ->
@@ -1336,7 +1336,7 @@ is executing this coroutine when [debugging mode](#debugging-coroutines-and-thre
13361336
The following example demonstrates this concept:
13371337

13381338
```kotlin
1339-
fun log(msg: String) = println("[${Thread.currentThread().name}] $msg")
1339+
fun log(msg: String) = Thread.currentThread().run { println("[$name @coroutine#$id] $msg") }
13401340

13411341
fun main(args: Array<String>) = runBlocking(CoroutineName("main")) {
13421342
log("Started main coroutine")

0 commit comments

Comments
 (0)