@@ -46,14 +46,13 @@ interface ActorJob<in E> : SendChannel<E> {
4646
4747/* *
4848 * 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
5050 * object can be used to [send][SendChannel.send] messages to this coroutine.
5151 *
5252 * The scope of the coroutine contains [ActorScope] interface, which implements
5353 * both [CoroutineScope] and [ReceiveChannel], so that coroutine can invoke
5454 * [receive][ReceiveChannel.receive] directly. The channel is [closed][SendChannel.close]
5555 * when the coroutine completes.
56- * The running coroutine is cancelled when the its job is [cancelled][Job.cancel].
5756 *
5857 * The [context] for the new coroutine can be explicitly specified.
5958 * See [CoroutineDispatcher] for the standard context implementations that are provided by `kotlinx.coroutines`.
@@ -66,15 +65,53 @@ interface ActorJob<in E> : SendChannel<E> {
6665 * By default, the coroutine is immediately scheduled for execution.
6766 * Other options can be specified via `start` parameter. See [CoroutineStart] for details.
6867 * 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.
7270 *
7371 * Uncaught exceptions in this coroutine close the channel with this exception as a cause and
7472 * the resulting channel becomes _failed_, so that any attempt to send to such a channel throws exception.
7573 *
7674 * See [newCoroutineContext] for a description of debugging facilities that are available for newly created coroutine.
7775 *
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+ *
78115 * @param context context of the coroutine. The default value is [DefaultDispatcher].
79116 * @param capacity capacity of the channel's buffer (no buffer by default).
80117 * @param start coroutine start option. The default value is [CoroutineStart.DEFAULT].
0 commit comments