diesieben07
02/28/2018, 12:51 PMSendChannel which might be closed? Do I have to just catch the ClosedSendChannelException?Jonathan
02/28/2018, 12:52 PMdiesieben07
02/28/2018, 12:53 PMclose the correct way?Jonathan
02/28/2018, 12:54 PMisClosedForSend to know if it is currently close.Jonathan
02/28/2018, 12:55 PMBroadcastChannel?Jonathan
02/28/2018, 12:55 PMdiesieben07
02/28/2018, 12:55 PMisClosedForSend, but that's a race condition, is it not (tocttou)?diesieben07
02/28/2018, 12:56 PMJonathan
02/28/2018, 12:57 PMproduce. this function will handle everything, like closing when done, when cancelled and when failed.diesieben07
02/28/2018, 12:59 PMsendIfPossibleOrReturnFalse method.diesieben07
02/28/2018, 1:00 PMdo {
val element = TODO() // compute next element
} while (channel.sendIfPossibleOrReturnFalse(element))diesieben07
02/28/2018, 1:01 PMJonathan
02/28/2018, 1:02 PMproduce {
while(isActive) {
val element = TODO()
send(element)
}
}
Then produce will handle it for youdiesieben07
02/28/2018, 1:02 PMTODO is? Then send will throw...Jonathan
02/28/2018, 1:04 PMsend will throw a CancelledException.diesieben07
02/28/2018, 1:04 PMdiesieben07
02/28/2018, 1:04 PMdiesieben07
02/28/2018, 1:05 PMdiesieben07
02/28/2018, 1:07 PMproduce approach, how do I terminate the underlying thread (newSingleThreadContext) when the channel is closed?Jonathan
02/28/2018, 1:07 PMCancelledException will be consumed by produce as it is a normal reason to be terminated. If you use resources you should anyway always ensure they are closed in case of exception. (same as if you wouldn't use coroutines)diesieben07
02/28/2018, 1:08 PMJonathan
02/28/2018, 1:08 PMnewSingleThreadContext you have of course to ensure that is terminated even in case of exception.diesieben07
02/28/2018, 1:08 PMdiesieben07
02/28/2018, 1:09 PMuse will not work, will it?Jonathan
02/28/2018, 1:10 PMdiesieben07
02/28/2018, 1:11 PMnewSingleThreadContext().use { ctx ->
return produce(ctx) {
}
}
won't work, right?Jonathan
02/28/2018, 1:11 PMJonathan
02/28/2018, 1:12 PMdiesieben07
02/28/2018, 1:12 PMdiesieben07
02/28/2018, 1:13 PMproduce?Jonathan
02/28/2018, 1:14 PMfun startProducer() {
val context = newSingleThreadContext("my dispatcher")
val result = produce(context) {
context.use {
send(1)
send(2)
send(3)
}
}
}Jonathan
02/28/2018, 1:14 PMdiesieben07
02/28/2018, 1:14 PMcoroutineContext as a parameter.diesieben07
02/28/2018, 1:15 PMJonathan
02/28/2018, 1:15 PMbj0
02/28/2018, 4:35 PMproduce {
newSingleThreadContet("dispatch").use {
while(isActive)
send(TODO())
}
}Jonathan
02/28/2018, 4:36 PMproduce use CommonPoolbj0
02/28/2018, 4:37 PMbj0
02/28/2018, 4:37 PMwithContext in therebj0
02/28/2018, 4:37 PMwhile loopJonathan
02/28/2018, 4:38 PMproducebj0
02/28/2018, 4:41 PMbj0
02/28/2018, 4:42 PMJonathan
02/28/2018, 4:46 PMJonathan
02/28/2018, 4:46 PMbj0
02/28/2018, 4:50 PMJonathan
02/28/2018, 4:56 PMval MyThreadPool = newSingleThreadPool("my thread pool") and use it for the whole life of the application. calling produce(MyThreadPool) {}diesieben07
02/28/2018, 5:00 PMCoroutineContext into my own method and then pass it on to produce.bj0
02/28/2018, 5:07 PMdiesieben07
02/28/2018, 5:08 PMnewSingleThreadContext().use {}. It is closed properly.bj0
02/28/2018, 5:18 PM