Last active
March 22, 2018 00:21
-
-
Save twyatt/c51f81d763a6ee39657233fa725f5435 to your computer and use it in GitHub Desktop.
Testing Kotlin Coroutines when using `newSingleThreadContext` (Thread is not being cleaned up)
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
import kotlinx.coroutines.experimental.channels.actor | |
import kotlinx.coroutines.experimental.channels.consumeEach | |
import kotlinx.coroutines.experimental.newSingleThreadContext | |
import kotlinx.coroutines.experimental.runBlocking | |
val threads: Set<Thread> | |
get() = Thread.getAllStackTraces().keys | |
val threadNames: List<String> | |
get() = threads.map { it.name } | |
fun main(args: Array<String>) = runBlocking { | |
println("threads = $threadNames") | |
val actor = actor<Int>(newSingleThreadContext("TestThread")) { | |
println("actor start") | |
consumeEach { item -> | |
println("item = $item") | |
} | |
println("actor end") | |
} | |
println("threads = $threadNames") | |
println("actor.close()") | |
actor.close() | |
println("threads = $threadNames") | |
println("thread sleep") | |
Thread.sleep(2_000L) | |
println("threads = $threadNames") | |
} |
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
threads = [Signal Dispatcher, Reference Handler, Finalizer, main, Monitor Ctrl-Break] | |
actor start | |
threads = [TestThread, Signal Dispatcher, Reference Handler, Finalizer, main, Monitor Ctrl-Break] | |
actor.close() | |
actor end | |
threads = [TestThread, Signal Dispatcher, Reference Handler, Finalizer, main, Monitor Ctrl-Break] | |
thread sleep | |
threads = [TestThread, Signal Dispatcher, Reference Handler, Finalizer, main, Monitor Ctrl-Break] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Per @elizarov in #coroutines at https://kotlinlang.slack.com/: