# Coroutine dispatcher confined to a single thread

**URL:** <https://discuss.kotlinlang.org/t/coroutine-dispatcher-confined-to-a-single-thread/17978>\
**Category:** Language Design\
**Created:** [June 5, 2020, 9:57pm UTC](https://discuss.kotlinlang.org/t/coroutine-dispatcher-confined-to-a-single-thread/17978 "2020-06-05T21:57:25Z")\
**Posts on this page:** 1\
**Showing post:** 10

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [June 7, 2020, 6:30am UTC](https://discuss.kotlinlang.org/t/coroutine-dispatcher-confined-to-a-single-thread/17978/10 "2020-06-07T06:30:39Z")

</div>

You must never use `runBlocking` inside the coroutine. Especially inside default dispatcher because it will eventually block the whole pool. The feature you are searching for is called context switch and achieved by `withContext` function or additional CoroutineContext `launch` argument. Like this:

```kotlin
val singleDispatcher = Executors.newSigleThreadExecutor().asCoroutineDispatcher()
// ...
launch(Dispatchers.Default){
  // do something multithreaded...
  val res = withContext(singleDispatcher){
    // do somthething on a single thread...
  }//return to Default
}
```

---

_[View the full topic](https://discuss.kotlinlang.org/t/coroutine-dispatcher-confined-to-a-single-thread/17978)._
