Skip to content

Instantly share code, notes, and snippets.

@kariyayo
Created August 23, 2022 02:54
Show Gist options
  • Save kariyayo/cbd0e69a37b133e7191d9466b6b073e4 to your computer and use it in GitHub Desktop.
Save kariyayo/cbd0e69a37b133e7191d9466b6b073e4 to your computer and use it in GitHub Desktop.
import kotlinx.coroutines.*
class Watch() {
private var start: Long? = null
fun t(): Long {
return if (start == null) {
start = java.time.Instant.now().toEpochMilli()
0L
} else {
java.time.Instant.now().toEpochMilli() - (start ?: 0)
}
}
}
val watch = Watch()
fun p(s: String) {
println("t=${"%-4s".format(watch.t())} ${s}")
}
fun main() {
p("START")
runBlocking {
// A
p("A: before")
delay(1000) // 1000 ミリ秒ブロッキングが発生する処理を行うとする
p("A: after")
// B
p("B: before")
delay(2000) // 2000 ミリ秒ブロッキングが発生する処理、例えば HTTP 通信する
p("B: after")
}
p("END")
}
// ↓出力
// t=0 START
// t=110 A: before
// t=1119 A: after
// t=1119 B: before
// t=3119 B: after
// t=3120 END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment