produce
fun <E> produce(
context: CoroutineContext = DefaultDispatcher,
capacity: Int = 0,
parent: Job? = null,
block: suspend ProducerScope<E>.() -> Unit
): ReceiveChannel<E>
(source)Platform and version requirements: JVM
Launches new coroutine to produce a stream of values by sending them to a channel and returns a reference to the coroutine as a ReceiveChannel. This resulting object can be used to receive elements produced by this coroutine.
The scope of the coroutine contains ProducerScope interface, which implements both CoroutineScope and SendChannel, so that coroutine can invoke send directly. The channel is closed when the coroutine completes. The running coroutine is cancelled when its receive channel is cancelled.
The context for the new coroutine can be explicitly specified.
See CoroutineDispatcher for the standard context implementations that are provided by kotlinx.coroutines
.
The context of the parent coroutine from its scope may be used,
in which case the Job of the resulting coroutine is a child of the job of the parent coroutine.
The parent job may be also explicitly specified using parent parameter.
If the context does not have any dispatcher nor any other ContinuationInterceptor, then DefaultDispatcher is used.
Uncaught exceptions in this coroutine close the channel with this exception as a cause and the resulting channel becomes failed, so that any attempt to receive from such a channel throws exception.
See newCoroutineContext for a description of debugging facilities that are available for newly created coroutine.
Parameters
context
- context of the coroutine. The default value is DefaultDispatcher.
capacity
- capacity of the channel’s buffer (no buffer by default).
parent
- explicitly specifies the parent job, overrides job from the context (if any).*
block
- the coroutine code.