I need to spawn 10 worker threads which will call ...
# coroutines
c
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
Why can't you call them directly with
withContext(<http://Dispatchers.IO|Dispatchers.IO>) { blockingCall() }
?
c
I want to have a limited number of pending operations and no shared state.
d
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
executing
yes I am willing to share a queue/channel
d
You can use a
Semaphore
for the former,
r
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.