Created
December 4, 2023 09:03
-
-
Save enshahar/9be110c52279292d69b3d85dd1cad424 to your computer and use it in GitHub Desktop.
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 kotlinx.coroutines.ExecutorCoroutineDispatcher | |
import kotlinx.coroutines.asCoroutineDispatcher | |
import java.util.concurrent.ExecutorService | |
import java.util.concurrent.Executors | |
import kotlin.coroutines.* | |
object WithoutKotlinx3 { | |
var saved: MutableList<Any> = mutableListOf() | |
class MyCont2<T>(override val context: CoroutineContext) : Continuation<T> { | |
override fun resumeWith(result: Result<T>) { | |
if (saved.isNotEmpty()) { | |
val cont = saved.removeFirst() as Continuation<Unit> | |
//println("resume saved: ${cont}") | |
cont.resume(Unit) | |
} | |
} | |
} | |
class ContPoolTermination<T>(override val context: ExecutorCoroutineDispatcher) : Continuation<T> { | |
override fun resumeWith(result: Result<T>) { | |
if (saved.isNotEmpty()) { | |
val cont = saved.removeFirst() as Continuation<Unit> | |
println("resume saved from ContPoolTermination: ${cont}") | |
cont.resume(Unit) | |
//println("======================== after final continuation resumed") | |
} | |
//println("======================== final continuation resumed ${Thread.currentThread().name}") | |
} | |
} | |
fun ExecutorCoroutineDispatcher.runBlocking(block: suspend CoroutineContext.() -> Unit) { | |
//println("before start coroutine: ${Thread.currentThread().name}") | |
block.startCoroutine(this, ContPoolTermination<Unit>(this)) | |
//println("after start coroutine: ${Thread.currentThread().name}") | |
} | |
suspend fun CoroutineContext.launch(block: suspend CoroutineContext.() -> Unit) { | |
val blockCoroutine = block.createCoroutine(this, MyCont2<Unit>(this)) | |
//println("launch: add ${blockCoroutine}") | |
suspendCoroutine<Unit> { | |
saved.add(it) | |
saved.add(blockCoroutine) | |
if (saved.isNotEmpty()) { | |
(saved.removeFirst() as Continuation<Unit>).resume(Unit) | |
} | |
//println("after resume-----------------------------------------------") | |
} | |
//println("after suspendCoroutine -----------------------------------------------") | |
} | |
fun main() { | |
fun t() = java.time.LocalTime.now() | |
println("start ${t()}") | |
val singleThreadPool = Executors.newFixedThreadPool(1) | |
singleThreadPool.asCoroutineDispatcher().runBlocking{ | |
println("thread = ${Thread.currentThread().name}") | |
launch { | |
launch { | |
var i=0 | |
while(i++ < 100) { | |
launch { | |
println("1 - $i") | |
println("thread = ${Thread.currentThread().name}") | |
} | |
} | |
} | |
launch { | |
var i=0 | |
while(i++ < 100) { | |
launch { | |
println("2 - $i") | |
println("thread = ${Thread.currentThread().name}") | |
} | |
} | |
} | |
} | |
println("end thread = ${Thread.currentThread().name}") | |
} | |
println("after launch ${t()}") | |
} | |
} | |
fun main() { | |
WithoutKotlinx3.main() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment