launch
fun launch(
context: CoroutineContext = DefaultDispatcher,
start: CoroutineStart = CoroutineStart.DEFAULT,
parent: Job? = null,
block: suspend CoroutineScope.() -> Unit
): Job
(source)Launches new coroutine without blocking current thread and returns a reference to the coroutine as a Job. The coroutine is cancelled when the resulting job 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.
By default, the coroutine is immediately scheduled for execution.
Other options can be specified via start
parameter. See CoroutineStart for details.
An optional start parameter can be set to CoroutineStart.LAZY to start coroutine lazily. In this case,
the coroutine Job is created in new state. It can be explicitly started with start function
and will be started implicitly on the first invocation of join.
Uncaught exceptions in this coroutine cancel parent job in the context by default
(unless CoroutineExceptionHandler is explicitly specified), which means that when launch
is used with
the context of another coroutine, then any uncaught exception leads to the cancellation of parent coroutine.
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.
start
- coroutine start option. The default value is CoroutineStart.DEFAULT.
parent
- explicitly specifies the parent job, overrides job from the context (if any).
block
- the coroutine code.