September 29, 2020
fun main() = runBlocking {
val job = launch {
repeat(1000) { i ->
println("job : I'm sleeping $i ...")
delay(500)
}
}
delay(1300)
println("main : I'm tired of waiting!")
job.cancel()
println("main : Now I can quit.")
}
// job : I'm sleeping 0 ...
// job : I'm sleeping 1 ...
// job : I'm sleeping 2 ...
// main : I'm tired of waiting!
// main : Now I can quit.
fun main() = runBlocking {
// 위의 에제에서는 코루틴 내부에 delay라는 suspend함수가 존재하지만 예제2는 없음
val startTime = System.currentTimeMillis()
val job = launch(Dispatchers.Default) {
var nextPrintTime = startTime
var i = 0
while(i < 5) {
// 현재 밀리초가 초기값보다 크다면 진행하고, 초기값에 0.5초 더해줌
// 현재 밀리초가 초기값보다 더 작다가 시간이 흐르면 초기값보다 커져서 다시 진행
// 즉, 0.5초마다 반복이 됨
if(System.currentTimeMillis() >= nextPrintTime) {
println("job : I'm sleeping ${i++} ...")
nextPrintTime += 500
}
}
}
delay(1300)
println("main : I'm tired of waiting!")
job.cancelAndJoin()
println("main : Now I can quit.")
}
// job : I'm sleeping 0 ...
// job : I'm sleeping 1 ...
// job : I'm sleeping 2 ...
// main : I'm tired of waiting!
// job : I'm sleeping 3 ...
// job : I'm sleeping 4 ...
// main : Now I can quit.
fun main() = runBlocking {
val startTime = System.currentTimeMillis()
val job = launch(Dispatchers.Default) {
try {
var nextPrintTime = startTime
var i = 0
while(i < 5) {
if(System.currentTimeMillis() >= nextPrintTime) {
yield()
println("job : I'm sleeping ${i++} ...")
nextPrintTime += 500
}
}
}catch (e : Exception) {
println("Suspended by $e")
}
}
delay(1300)
println("main : I'm tired of waiting!")
job.cancelAndJoin()
println("main : Now I can quit.")
}
// job : I'm sleeping 0 ...
// job : I'm sleeping 1 ...
// job : I'm sleeping 2 ...
// main : I'm tired of waiting!
// Suspended by kotlinx.coroutines.JobCancellationException: StandaloneCoroutine was cancelled; job=StandaloneCoroutine{Cancelling}@4f591ba8
// main : Now I can quit.
fun main() = runBlocking {
val startTime = System.currentTimeMillis()
val job = launch(Dispatchers.Default) {
var nextPrintTime = startTime
var i = 0
while(isActive) {
if(System.currentTimeMillis() >= nextPrintTime) {
yield()
println("job : I'm sleeping ${i++} ...")
nextPrintTime += 500
}
}
}
delay(1300)
println("main : I'm tired of waiting!")
job.cancelAndJoin()
println("main : Now I can quit.")
}
// job : I'm sleeping 0 ...
// job : I'm sleeping 1 ...
// job : I'm sleeping 2 ...
// main : I'm tired of waiting!
// main : Now I can quit.
fun main() = runBlocking {
val job = launch {
try {
repeat(1000) { i ->
println("job : I'm sleeping $i ...")
delay(500)
}
} finally {
println("job : I'm running finally")
}
}
delay(1300)
println("main : I'm tired of waiting!")
job.cancelAndJoin()
println("main : Now I can quit.")
}
// job : I'm sleeping 0 ...
// job : I'm sleeping 1 ...
// job : I'm sleeping 2 ...
// main : I'm tired of waiting!
// job : I'm running finally
// main : Now I can quit.
fun main() = runBlocking {
val job = launch {
try {
repeat(1000) { i ->
println("job : I'm sleeping $i ...")
delay(500)
}
} finally {
withContext(NonCancellable) {
println("job : I'm running finally")
delay(1000)
println("job : And I've just delayed for 1 sec because I'm non-cancellable")
}
}
}
delay(1300)
println("main : I'm tired of waiting!")
job.cancelAndJoin()
println("main : Now I can quit.")
}
// job : I'm sleeping 0 ...
// job : I'm sleeping 1 ...
// job : I'm sleeping 2 ...
// main : I'm tired of waiting!
// job : I'm running finally
// job : And I've just delayed for 1 sec because I'm non-cancellable
// main : Now I can quit.
fun main() = runBlocking {
val result = withTimeoutOrNull(1300) {
repeat(1000) { i ->
println("I'm sleeping $i ...")
delay(500)
}
"Done"
}
println("Result is $result")
}
// I'm sleeping 0 ...
// I'm sleeping 1 ...
// I'm sleeping 2 ...
// Result is null