I've been holding off from coroutines a while now....
# coroutines
b
I've been holding off from coroutines a while now. Every time I tried adding them, I'd run into severe headwinds. Today I tried again, and basically just converted my executor service like this:
Copy code
-    executorService: ExecutorService = Executors.newCachedThreadPool(Executors.defaultThreadFactory())
+    executorService: ExecutorService = Executors.newCachedThreadPool(Executors.defaultThreadFactory()).asCoroutineDispatcher().executor as ExecutorService,
and bam! Performance up by 40% on my heaviest function.
e
Very strange. It should not be making a difference. Double-check your benchmarks.
b
The man himself! But why shouldn't that make a difference?
e
Copy code
val originalExecutor = Executors.newCachedThreadPool(Executors.defaultThreadFactory())
val resultingExecutor = originalExecutor.asCoroutineDispatcher().executor as ExecutorService
println(resultingExecutor === originalExecutor) // prints true
b
aww
I should have looked closer. So, what is the proper way to convert an executor service to a coroutine version?
e
It is the proper way. What are you trying to achieve?
b
Trying to use the exact same interface that ExecutorService provides for threads, but for coroutines, i.e.
foo.submit ()
e
For what?
b
for a non-ssl server, an ssl-server, a blockingLinkedQueue, and for handling client requests. I pass one ExecutorService around the entire application to be used. Currently it works fine with a cached pool of threads but I was hoping to just sprinkle some magic coroutines in to avoid the 1-1 dependency on OS threads.
e
It does not work this way. There is no magic
b
😢
I guess I was hoping there was a way to incorporate coroutines without a total overhaul of the codebase, and to date I haven't found a way to do this.