public class ProduceKt
Modifier and Type | Method and Description |
---|---|
static <E> ReceiveChannel<E> |
produce(CoroutineScope $receiver,
NonExistentClass context,
int capacity,
kotlin.jvm.functions.Function2<? super kotlinx.coroutines.channels.ProducerScope<? super E>,? super kotlin.coroutines.experimental.Continuation<? super kotlin.Unit>,? extends java.lang.Object> block)
Launches new coroutine to produce a stream of values by sending them to a channel
and returns a reference to the coroutine as a
interface ReceiveChannel . This resulting
object can be used to ReceiveChannel.receive elements produced by this coroutine. |
public static <E> ReceiveChannel<E> produce(CoroutineScope $receiver, NonExistentClass context, int capacity, kotlin.jvm.functions.Function2<? super kotlinx.coroutines.channels.ProducerScope<? super E>,? super kotlin.coroutines.experimental.Continuation<? super kotlin.Unit>,? extends java.lang.Object> block)
Launches new coroutine to produce a stream of values by sending them to a channel
and returns a reference to the coroutine as a interface ReceiveChannel
. This resulting
object can be used to ReceiveChannel.receive
elements produced by this coroutine.
The scope of the coroutine contains interface ProducerScope
interface, which implements
both interface CoroutineScope
and interface SendChannel
, so that coroutine can invoke
SendChannel.send
directly. The channel is SendChannel.close
when the coroutine completes.
The running coroutine is cancelled when its receive channel is ReceiveChannel.cancel
.
Coroutine context is inherited from a interface CoroutineScope
, additional context elements can be specified with context argument.
If the context does not have any dispatcher nor any other ContinuationInterceptor, then Dispatchers.getDefault
is used.
The parent job is inherited from a interface CoroutineScope
as well, but it can also be overridden
with corresponding coroutineContext element.
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.
The kind of the resulting channel depends on the specified capacity parameter.
See interface Channel
interface documentation for details.
See newCoroutineContext for a description of debugging facilities that are available for newly created coroutine.
Note: This is an experimental api. Behaviour of producers that work as children in a parent scope with respect to cancellation and error handling may change in the future.
context
- additional to CoroutineScope.getCoroutineContext
context of the coroutine.capacity
- capacity of the channel's buffer (no buffer by default).block
- the coroutine code.interface ReceiveChannel
,
ReceiveChannel.receive
,
interface ProducerScope
,
interface CoroutineScope
,
interface SendChannel
,
SendChannel.send
,
SendChannel.close
,
ReceiveChannel.cancel
,
interface CoroutineScope
,
ContinuationInterceptor,
Dispatchers.getDefault
,
interface CoroutineScope
,
coroutineContext,
interface Channel