question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Convert `CoroutineContext` into `Executor`

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
elizarovcommented, Aug 27, 2019

@ZakTaccardi You can write the following simple extension for your use-case:

fun CoroutineContext.asExecutor(): Exector = 
    (get(ContinuationInterceptor) as CoroutineDispatcher).asExecutor()
0reactions
ZakTaccardicommented, Aug 27, 2019

@zach-klippenstein For my use case, it would be fine to throw if the CoroutineContext does not contain a CoroutineDispatcher. I do something like the following:

AppDispatchers(
  val main: CoroutineContext = Dispatchers.Main,
  val default: CoroutineContext = Dispatchers.Default,
  val io: CoroutineContext = Dispatchers.IO,
)

CoroutineContext is preferred over CoroutineDispatcher as a type because it allows us to do something like dispatchers.copy(default = default + CoroutineExceptionHandler { ..}) (basically just the Liskov Substituion Principle)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found