sitepodmatt
07/06/2017, 7:20 AMelizarov
07/06/2017, 7:35 AMelizarov
07/06/2017, 7:35 AMval queue = LinkedBlockingQueue<Note>()
var closed = false
init {
async(CommonPool) {
while (!closed) {
while (!closed && queue.isEmpty()) {
delay(50)
}
while (!closed && !queue.isEmpty()) {
play(queue.take())
}
}
}
}
I don't like delay(50)
and I think it's not sufficient.
3 replies
Channels indeed provide a direct replacement for blocking queues when you work with coroutineselizarov
07/06/2017, 7:38 AMjkbbwr
07/07/2017, 1:31 PMs1m0nw1
07/07/2017, 1:42 PMjkbbwr
07/07/2017, 1:43 PMs1m0nw1
07/07/2017, 2:06 PMvaskir
07/07/2017, 5:04 PMilya.gorbunov
07/08/2017, 12:36 AMlaunch(CommonPool) {}.join()
inside runBlocking(CommonPool) {}
in this example:
runBlocking(CommonPool) { //(1)
launch(CommonPool) {
sendEmailSuspending() //(2)
LOG.debug("Email sent successfully.")
}.join() //(9)
}
s1m0nw1
07/08/2017, 6:59 AMmain
would terminate before all outputs can be observed. I get what you mean though and updated it to:
runBlocking(CommonPool) { //(1)
val job = launch(CommonPool) {
sendEmailSuspending() //(2)
println("Email sent successfully.")
}
job.join() //(9)
println("Finished")
}
Now we want to do something after the email was sent which makes join reasonable. All right?sitepodmatt
07/10/2017, 7:46 AMs1m0nw1
07/10/2017, 9:04 AMkarelpeeters
07/10/2017, 9:49 AMilya.gorbunov
07/10/2017, 8:42 PMrogeralsing
07/11/2017, 5:25 AMelizarov
07/11/2017, 7:57 AMTask.FromResult(...)
, because there is no much need to define functions that return Deferred<T>
(like Task<T>
in .NET). Writing suspend
functions with natural return types is so much nicer and you just return
from them normally and invoke them normally. However, having the corresponding constructors for Deferred
would not harm either.rogeralsing
07/11/2017, 11:00 AMProto.Actor C# ported to Kotlin :-)▾
rogeralsing
07/11/2017, 11:01 AMrogeralsing
07/11/2017, 11:01 AMnoctarius
07/11/2017, 11:04 AMrogeralsing
07/11/2017, 11:05 AMelizarov
07/11/2017, 11:24 AMrogeralsing
07/11/2017, 11:31 AMrafal
07/11/2017, 2:44 PMrafal
07/11/2017, 2:44 PMfun <T> ReceiveChannel<T>.reduce(reducer: (T, T) -> T) = produce<T>(Unconfined) {
var prev: T? = null
for (elem in this@reduce) {
if (prev == null) {
prev = elem
send(elem)
} else {
val new = reducer(prev, elem)
prev = new
send(new)
}
}
}
When run in debug mode everything works fine. However when run normally reading from prev
always returns null
- I suppose some "optimisation" is taking place in non-debug builds.karelpeeters
07/11/2017, 4:30 PMrafal
07/11/2017, 4:41 PMnewSingleThreadContext
(whole produce
code is run inside one thread), the issue persistselizarov
07/11/2017, 7:18 PMrafal
07/12/2017, 8:32 AM