Convert `CoroutineContext` into `Executor`
See original GitHub issueI have to use an API that accepts an Executor
as a parameter. How can I create an Executor
that is backed by Dispatchers.Default
?
Can a CoroutineDispatcher.asExecutor(): Executor
function be added to support this use case?
fun externalApi(executor: Executor)
fun example() {
val executor: Executor = Dispatchers.Default.asExecutor()
externalApi(executor)
}
Even better would be a fun CoroutineContext.asExecutor(): Executor
function. I would imagine if the receiver CoroutineContext
does not have a ContinuationInterceptor
, this function would throw.
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
Coroutine context and dispatchers - Kotlin
Coroutines always execute in some context represented by a value of the CoroutineContext type, defined in the Kotlin standard library.
Read more >Kotlin: Create custom CoroutineContext - Stack Overflow
The recommended way to create a CoroutineContext is now through ... Int): CoroutineScope { val context: CoroutineContext = Executors.
Read more >Coroutine Context and Dispatchers | Baeldung on Kotlin
We have several coroutine builder functions: launch and async, extensions of CoroutineScope, and runBlocking. 3. Coroutine Context.
Read more >Kotlin Coroutines dispatchers - Kt. Academy
In Kotlin coroutines, CoroutineContext determines on which thread a certain ... the ExecutorService or Executor interfaces, which we can transform into a ...
Read more >Kotlin Coroutines on Android: Things I Wish I Knew at the ...
You Can Convert Executors Into CoroutineDispatchers. Executors are generally used as a way to manage various thread pools. If you are ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@ZakTaccardi You can write the following simple extension for your use-case:
@zach-klippenstein For my use case, it would be fine to throw if the
CoroutineContext
does not contain aCoroutineDispatcher
. I do something like the following:CoroutineContext
is preferred overCoroutineDispatcher
as a type because it allows us to do something likedispatchers.copy(default = default + CoroutineExceptionHandler { ..})
(basically just the Liskov Substituion Principle)