https://kotlinlang.org logo
#coroutines
Title
# coroutines
c

coder82

08/13/2019, 10:31 AM
I need to spawn 10 worker threads which will call blocking legacy services. I also need them to have a bounded queue from which they read from, but I don't want to go through the effort of specifying a fan out channel which they read from and then having to check the message type all the time (10 actors reading and responding pattern). Rather I'd create a custom ThreadPoolExecutor where I can make the queue bounded and submit tasks directly without having to deal with messages flowing through a channel. What do you reckon? The problem of the actor model is that I need to define a lot of data classes and check which message I get so that I know what to do in the worker, while submitting the task directly is more convenient. Unless I miss something or I get some hint here 😄
d

Dominaezzz

08/13/2019, 11:11 AM
Why can't you call them directly with
withContext(<http://Dispatchers.IO|Dispatchers.IO>) { blockingCall() }
?
c

coder82

08/13/2019, 11:56 AM
I want to have a limited number of pending operations and no shared state.
d

Dominaezzz

08/13/2019, 11:57 AM
Pending or executing?
No shared state or no shared mutable state? You're going to have to share something, if you want to pass data back and forth.
c

coder82

08/13/2019, 12:46 PM
executing
yes I am willing to share a queue/channel
d

Dominaezzz

08/13/2019, 12:48 PM
You can use a
Semaphore
for the former,
r

rocketraman

08/18/2019, 2:52 AM
You can create your own dispatcher e.g.:
Copy code
val legacyDispatcher = newFixedThreadPoolContext(10, "Blocking-Legacy-Dispatcher")
and then execute on that dispatcher via
withContext(legacyDispatcher) { ... }
But note the javadocs... this API will change in the future.
3 Views