Created
December 1, 2023 01:01
-
-
Save enshahar/e0dc8f426da0b5374119a006529b8c98 to your computer and use it in GitHub Desktop.
1st implementation of launch (failed)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package learningtest | |
import kotlin.coroutines.* | |
private class MyCont<T>(override val context: CoroutineContext): Continuation<T> { | |
override fun resumeWith(result: Result<T>) { | |
println("Resume with: ${result}") | |
} | |
} | |
fun <T> CoroutineContext.launch(block: suspend CoroutineContext.()->T) = | |
block.startCoroutine(this, MyCont(this)) | |
suspend fun test(msg: String, a:Int): Int { | |
println("$msg - 1") | |
delay(10) | |
println("$msg - 2") | |
delay(10) | |
println("$msg - 3") | |
return a * 2 | |
} | |
suspend fun delay(t: Int) { | |
val start = System.currentTimeMillis() | |
var i = 0 | |
while(true) { | |
val now = System.currentTimeMillis() | |
i++ | |
if(i%10_000_000 == 0) println("busy waiting: ${"%,d".format(i)}") | |
if(now - start >= t) return | |
} | |
} | |
fun main() { | |
fun t() = java.time.LocalTime.now() | |
println("start ${t()}") | |
EmptyCoroutineContext.launch { | |
launch { | |
println("${test("1st", 30)}") | |
println("after 1st launch ${t()}") | |
} | |
launch { | |
println("${test("2nd", 30)}") | |
println("after 2nd launch ${t()}") | |
} | |
} | |
println("after launch ${t()}") | |
Thread.sleep(4000) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
실행 결과:
실패함!