Skip to content

Commit 40a2560

Browse files
committed
Improve CoroutineDispatcher documentation
1 parent 493b0a6 commit 40a2560

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

kotlinx-coroutines-core/common/src/CoroutineDispatcher.kt

+13-1
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,14 @@ public abstract class CoroutineDispatcher :
3333
* Returns `true` if execution shall be dispatched onto another thread.
3434
* The default behaviour for most dispatchers is to return `true`.
3535
*
36+
* This method should never be used from general code, it is used only by `kotlinx.coroutines`
37+
* internals and its contract with the rest of API is an implementation detail.
38+
*
3639
* UI dispatchers _should not_ override `isDispatchNeeded`, but leave a default implementation that
3740
* returns `true`. To understand the rationale beyond this recommendation, consider the following code:
3841
*
3942
* ```kotlin
40-
* fun asyncUpdateUI() = async(MainThread) {
43+
* fun asyncUpdateUI() = async(Dispatchers.Main) {
4144
* // do something here that updates something in UI
4245
* }
4346
* ```
@@ -60,13 +63,19 @@ public abstract class CoroutineDispatcher :
6063
* parameter that allows one to optionally choose C#-style [CoroutineStart.UNDISPATCHED] behaviour
6164
* whenever it is needed for efficiency.
6265
*
66+
* This method should be generally exception-safe, an exception thrown from this method
67+
* may leave the coroutines that use this dispatcher in the inconsistent and hard to debug state.
68+
*
6369
* **Note: This is an experimental api.** Execution semantics of coroutines may change in the future when this function returns `false`.
6470
*/
6571
@ExperimentalCoroutinesApi
6672
public open fun isDispatchNeeded(context: CoroutineContext): Boolean = true
6773

6874
/**
6975
* Dispatches execution of a runnable [block] onto another thread in the given [context].
76+
*
77+
* This method should be generally exception-safe, an exception thrown from this method
78+
* may leave the coroutines that use this dispatcher in the inconsistent and hard to debug state.
7079
*/
7180
public abstract fun dispatch(context: CoroutineContext, block: Runnable)
7281

@@ -85,6 +94,9 @@ public abstract class CoroutineDispatcher :
8594

8695
/**
8796
* Returns continuation that wraps the original [continuation], thus intercepting all resumptions.
97+
*
98+
* This method should be generally exception-safe, an exception thrown from this method
99+
* may leave the coroutines that use this dispatcher in the inconsistent and hard to debug state.
88100
*/
89101
public final override fun <T> interceptContinuation(continuation: Continuation<T>): Continuation<T> =
90102
DispatchedContinuation(this, continuation)

0 commit comments

Comments
 (0)