@@ -33,11 +33,14 @@ public abstract class CoroutineDispatcher :
33
33
* Returns `true` if execution shall be dispatched onto another thread.
34
34
* The default behaviour for most dispatchers is to return `true`.
35
35
*
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
+ *
36
39
* UI dispatchers _should not_ override `isDispatchNeeded`, but leave a default implementation that
37
40
* returns `true`. To understand the rationale beyond this recommendation, consider the following code:
38
41
*
39
42
* ```kotlin
40
- * fun asyncUpdateUI() = async(MainThread ) {
43
+ * fun asyncUpdateUI() = async(Dispatchers.Main ) {
41
44
* // do something here that updates something in UI
42
45
* }
43
46
* ```
@@ -60,13 +63,19 @@ public abstract class CoroutineDispatcher :
60
63
* parameter that allows one to optionally choose C#-style [CoroutineStart.UNDISPATCHED] behaviour
61
64
* whenever it is needed for efficiency.
62
65
*
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
+ *
63
69
* **Note: This is an experimental api.** Execution semantics of coroutines may change in the future when this function returns `false`.
64
70
*/
65
71
@ExperimentalCoroutinesApi
66
72
public open fun isDispatchNeeded (context : CoroutineContext ): Boolean = true
67
73
68
74
/* *
69
75
* 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.
70
79
*/
71
80
public abstract fun dispatch (context : CoroutineContext , block : Runnable )
72
81
@@ -85,6 +94,9 @@ public abstract class CoroutineDispatcher :
85
94
86
95
/* *
87
96
* 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.
88
100
*/
89
101
public final override fun <T > interceptContinuation (continuation : Continuation <T >): Continuation <T > =
90
102
DispatchedContinuation (this , continuation)
0 commit comments