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.BroadcastChannel
?diesieben07
02/28/2018, 12:55 PMisClosedForSend
, but that's a race condition, is it not (tocttou)?Jonathan
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.do {
val element = TODO() // compute next element
} while (channel.sendIfPossibleOrReturnFalse(element))
Jonathan
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 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 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 PMdiesieben07
02/28/2018, 1:12 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)
}
}
}
diesieben07
02/28/2018, 1:14 PMcoroutineContext
as a parameter.Jonathan
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 CommonPool
bj0
02/28/2018, 4:37 PMwithContext
in therewhile
loopJonathan
02/28/2018, 4:38 PMproduce
bj0
02/28/2018, 4:41 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