ec
01/19/2020, 4:05 PMstreetsofboston
01/19/2020, 4:18 PMSENDING 2
print-out.
The channel
is a rendez-vous channel and the first call to channel.send(...)
will suspend forever, since there is no active listener/consumer.
There is no active listener/consumer because the first runBlocking
never returns and the second runBlocking
is never called.ec
01/19/2020, 4:21 PMnewSingleThreadContext
would create a new ~thread for each coroutine so it wouldnt get blocked at Sending 2
streetsofboston
01/19/2020, 4:22 PMrunBlocking
is never called and no listener will listen.ec
01/19/2020, 4:23 PMnewSingleThreadContext
do then?streetsofboston
01/19/2020, 4:24 PMstreetsofboston
01/19/2020, 4:25 PMlaunch
instead, not runBlocking: val scope = CoroutineScope(newSingleThreadContext(); .... scope.launch { .... } .... scope.launch { ... }
ec
01/19/2020, 4:25 PMnewSingleThread..
ec
01/19/2020, 4:25 PMExecutors.newSingleThreadExecutor().execute {
runBlocking {
println("Listening")
for (i in channel) {
println("${Thread.currentThread()} RECEIVED $i")
}
}
}
I expected same from newSingleThread..
toostreetsofboston
01/19/2020, 4:25 PMrunBlocking
blocks until its lambda has finished. Look at the documentation of this function. It doesn't matter on which thread that code in the lambda is runningec
01/19/2020, 4:26 PMstreetsofboston
01/19/2020, 4:27 PMrunBlocking
blocks the thread on which the runBlocking is called.streetsofboston
01/19/2020, 4:27 PMrunBlocking
calls inside the .execute { ... }
blockec
01/19/2020, 4:28 PMnewSingleThread..
doesnt change the thread so it blocks now I get it thxstreetsofboston
01/19/2020, 4:29 PMrunBlocking
on which thread to run its code-block/lambda. The runBlocking itself does not change on which thread the following code is executed.ec
01/19/2020, 4:29 PMec
01/19/2020, 4:29 PMReceiveChannel<T>
from a regular func. Couldnt find the right construct. Create thread pool and go for runBlocking? Or is there anything better?streetsofboston
01/19/2020, 4:30 PMoffer
instead of send
.streetsofboston
01/19/2020, 4:32 PMstreetsofboston
01/19/2020, 4:32 PMscope.launch { ... your coroutine code here ... }
to build a coroutine and consume your channel from within those coroutinesec
01/19/2020, 4:36 PMReceiveChannel<T>
with that function.ec
01/19/2020, 4:44 PMCoroutineScope(<http://Dispatchers.IO|Dispatchers.IO>).launch{}
(channel will offer its values from network thats why I went with IO) seems approp. for my use case, thx for the help