@@ -46,14 +46,13 @@ interface ActorJob<in E> : SendChannel<E> {
46
46
47
47
/* *
48
48
* Launches new coroutine that is receiving messages from its mailbox channel
49
- * and returns a reference to the coroutine as an [ActorJob ]. The resulting
49
+ * and returns a reference to its mailbox channel as a [SendChannel ]. The resulting
50
50
* object can be used to [send][SendChannel.send] messages to this coroutine.
51
51
*
52
52
* The scope of the coroutine contains [ActorScope] interface, which implements
53
53
* both [CoroutineScope] and [ReceiveChannel], so that coroutine can invoke
54
54
* [receive][ReceiveChannel.receive] directly. The channel is [closed][SendChannel.close]
55
55
* when the coroutine completes.
56
- * The running coroutine is cancelled when the its job is [cancelled][Job.cancel].
57
56
*
58
57
* The [context] for the new coroutine can be explicitly specified.
59
58
* See [CoroutineDispatcher] for the standard context implementations that are provided by `kotlinx.coroutines`.
@@ -66,15 +65,53 @@ interface ActorJob<in E> : SendChannel<E> {
66
65
* By default, the coroutine is immediately scheduled for execution.
67
66
* Other options can be specified via `start` parameter. See [CoroutineStart] for details.
68
67
* An optional [start] parameter can be set to [CoroutineStart.LAZY] to start coroutine _lazily_. In this case,
69
- * the coroutine [Job] is created in _new_ state. It can be explicitly started with [start][Job.start] function
70
- * and will be started implicitly on the first invocation of [join][Job.join] or on a first message
71
- * [sent][SendChannel.send] to this coroutine's mailbox channel.
68
+ * it will be started implicitly on the first message
69
+ * [sent][SendChannel.send] to this actors's mailbox channel.
72
70
*
73
71
* Uncaught exceptions in this coroutine close the channel with this exception as a cause and
74
72
* the resulting channel becomes _failed_, so that any attempt to send to such a channel throws exception.
75
73
*
76
74
* See [newCoroutineContext] for a description of debugging facilities that are available for newly created coroutine.
77
75
*
76
+ * ### Using actors
77
+ *
78
+ * A typical usage of the actor builder looks like this:
79
+ *
80
+ * ```
81
+ * val c = actor {
82
+ * // initialize actor's state
83
+ * for (msg in channel) {
84
+ * // process message here
85
+ * }
86
+ * }
87
+ * // send messages to the actor
88
+ * c.send(...)
89
+ * ...
90
+ * // stop the actor when it is no longer needed
91
+ * c.close()
92
+ * ```
93
+ *
94
+ * ### Stopping and cancelling actors
95
+ *
96
+ * When the inbox channel of the actor is [closed][SendChannel.close] it sends a special "close token" to the actor.
97
+ * The actor still processes all the messages that were already sent and then "`for (msg in channel)`" loop terminates
98
+ * and the actor completes.
99
+ *
100
+ * If the actor needs to be aborted without processing all the messages that were already sent to it, then
101
+ * it shall be created with a parent job:
102
+ *
103
+ * ```
104
+ * val job = Job()
105
+ * val c = actor(parent = job) { ... }
106
+ * ...
107
+ * // abort the actor
108
+ * job.cancel()
109
+ * ```
110
+ *
111
+ * When actor's parent job is [cancelled][Job.cancel], then actor's job becomes cancelled. It means that
112
+ * "`for (msg in channel)`" and other cancellable suspending functions throw [CancellationException] and actor
113
+ * completes without processing remaining messages.
114
+ *
78
115
* @param context context of the coroutine. The default value is [DefaultDispatcher].
79
116
* @param capacity capacity of the channel's buffer (no buffer by default).
80
117
* @param start coroutine start option. The default value is [CoroutineStart.DEFAULT].
0 commit comments