CINXE.COM
Coroutines | Kotlin Documentation
<!DOCTYPE html SYSTEM "about:legacy-compat"> <html lang="en-US" data-preset="contrast" data-primary-color="#307FFF"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta charset="UTF-8"><meta name="built-on" content="2024-11-18T21:26:08.677701407"><meta name="build-number" content="3272"><script> (function (w, d, s, l, i) { w[l] = w[l] || []; w[l].push({'gtm.start': new Date().getTime(), event: 'gtm.js'}); var f = d.getElementsByTagName(s)[0], j = d.createElement(s), dl = l != 'dataLayer' ? '&l=' + l : ''; j.async = true; j.src = '//www.googletagmanager.com/gtm.js?id=' + i + dl; f.parentNode.insertBefore(j, f); })(window, document, 'script', 'dataLayer', 'GTM-5P98'); </script> <script src="static/v3/analytics.js"></script> <title>Coroutines | Kotlin Documentation</title><script type="application/json" id="virtual-toc-data">[{"id":"how-to-start","level":0,"title":"How to start","anchor":"#how-to-start"},{"id":"documentation","level":1,"title":"Documentation","anchor":"#documentation"},{"id":"tutorials","level":1,"title":"Tutorials","anchor":"#tutorials"},{"id":"sample-projects","level":0,"title":"Sample projects","anchor":"#sample-projects"}]</script><script type="application/json" id="topic-shortcuts"></script><link href="static/v3/app.css?v=6.11.0-footer" rel="stylesheet"><link rel="icon" type="image/svg" sizes="16x16" href="https://kotlinlang.org/assets/images/favicon.svg?v2"><link rel="icon" type="image/x-icon" sizes="32x32" href="https://kotlinlang.org/assets/images/favicon.ico?v2"><link rel="icon" type="image/png" sizes="96x96" href="https://kotlinlang.org/assets/images/apple-touch-icon.png?v2"><link rel="icon" type="image/png" sizes="300x300" href="https://kotlinlang.org/assets/images/apple-touch-icon-72x72.png?v2"><link rel="icon" type="image/png" sizes="500x500" href="https://kotlinlang.org/assets/images/apple-touch-icon-114x114.png?v2"><meta name="image" content="https://kotlinlang.org/assets/images/open-graph/docs.png"><!-- Open Graph --><meta property="og:title" content="Coroutines | Kotlin"><meta property="og:description" content=""><meta property="og:image" content="https://kotlinlang.org/assets/images/open-graph/docs.png"><meta property="og:site_name" content="Kotlin Help"><meta property="og:type" content="website"><meta property="og:locale" content="en_US"><meta property="og:url" content="https://kotlinlang.org/docs/coroutines-overview.html"><!-- End Open Graph --><!-- Twitter Card --><meta name="twitter:card" content="summary_large_image"><meta name="twitter:site" content="@kotlin"><meta name="twitter:title" content="Coroutines | Kotlin"><meta name="twitter:description" content=""><meta name="twitter:creator" content="@kotlin"><meta name="twitter:image:src" content="https://kotlinlang.org/assets/images/open-graph/docs.png"><!-- End Twitter Card --><!-- Schema.org WebPage --><script type="application/ld+json">{ "@context": "http://schema.org", "@type": "WebPage", "@id": "https://kotlinlang.org/docs/coroutines-overview.html#webpage", "url": "https://kotlinlang.org/docs/coroutines-overview.html", "name": "Coroutines | Kotlin", "description": "", "image": "https://kotlinlang.org/assets/images/open-graph/docs.png", "inLanguage":"en-US" }</script><!-- End Schema.org --><!-- Schema.org WebSite --><script type="application/ld+json">{ "@type": "WebSite", "@id": "https://kotlinlang.org/docs/#website", "url": "https://kotlinlang.org/docs/", "name": "Kotlin Help" }</script><!-- End Schema.org --><style>a[href="test-page.html"] { visibility: hidden; }</style></head><body data-id="coroutines-overview" data-main-title="Coroutines" data-article-props="{"seeAlsoStyle":"links"}" data-template="article" data-breadcrumbs="Concepts" data-edit-url="https://github.com/JetBrains/kotlin-web-site/edit/master/docs/topics/coroutines-overview.md"><div class="wrapper"><main class="panel _main"><header class="panel__header"><div class="container"><h3>Kotlin Help</h3><div class="panel-trigger"></div></div></header><section class="panel__content"><div class="container"><article class="article" data-shortcut-switcher="inactive"><h1 data-toc="coroutines-overview" id="coroutines-overview.md">Coroutines</h1><p id="-n0i83_2">Asynchronous or non-blocking programming is an important part of the development landscape. When creating server-side, desktop, or mobile applications, it's important to provide an experience that is not only fluid from the user's perspective, but also scalable when needed.</p><p id="-n0i83_3">Kotlin solves this problem in a flexible way by providing <a href="https://en.wikipedia.org/wiki/Coroutine" id="-n0i83_7" data-external="true" rel="noopener noreferrer">coroutine</a> support at the language level and delegating most of the functionality to libraries.</p><p id="-n0i83_4">In addition to opening the doors to asynchronous programming, coroutines also provide a wealth of other possibilities, such as concurrency and actors.</p><section class="chapter"><h2 id="how-to-start" data-toc="how-to-start">How to start</h2><p id="-n0i83_8">New to Kotlin? Take a look at the <a href="getting-started.html" id="-n0i83_11">Getting started</a> page.</p><section class="chapter"><h3 id="documentation" data-toc="documentation">Documentation</h3><ul class="list _bullet" id="-n0i83_12"><li class="list__item" id="-n0i83_13"><p><a href="coroutines-guide.html" id="-n0i83_19" data-tooltip="Kotlin provides only minimal low-level APIs in its standard library to enable other libraries to utilize coroutines. Unlike many other languages with similar capabilities, async and await are not keywords in Kotlin and are not even part of its standard library. Moreover, Kotlin's…">Coroutines guide</a></p></li><li class="list__item" id="-n0i83_14"><p><a href="coroutines-basics.html" id="-n0i83_20" data-tooltip="This section covers basic coroutine concepts.">Basics</a></p></li><li class="list__item" id="-n0i83_15"><p><a href="channels.html" id="-n0i83_21" data-tooltip="Deferred values provide a convenient way to transfer a single value between coroutines. Channels provide a way to transfer a stream of values.">Channels</a></p></li><li class="list__item" id="-n0i83_16"><p><a href="coroutine-context-and-dispatchers.html" id="-n0i83_22" data-tooltip="Coroutines always execute in some context represented by a value of the CoroutineContext type, defined in the Kotlin standard library.">Coroutine context and dispatchers</a></p></li><li class="list__item" id="-n0i83_17"><p><a href="shared-mutable-state-and-concurrency.html" id="-n0i83_23" data-tooltip="Coroutines can be executed parallelly using a multi-threaded dispatcher like the Dispatchers.Default. It presents all the usual parallelism problems. The main problem being synchronization of access to shared mutable state. Some solutions to this problem in the land of coroutines…">Shared mutable state and concurrency</a></p></li><li class="list__item" id="-n0i83_18"><p><a href="flow.html" id="-n0i83_24" data-tooltip="A suspending function asynchronously returns a single value, but how can we return multiple asynchronously computed values? This is where Kotlin Flows come in.">Asynchronous flow</a></p></li></ul></section><section class="chapter"><h3 id="tutorials" data-toc="tutorials">Tutorials</h3><ul class="list _bullet" id="-n0i83_25"><li class="list__item" id="-n0i83_26"><p><a href="async-programming.html" id="-n0i83_31">Asynchronous programming techniques</a></p></li><li class="list__item" id="-n0i83_27"><p><a href="coroutines-and-channels.html" id="-n0i83_32" data-tooltip="In this tutorial, you'll learn how to use coroutines in IntelliJ IDEA to perform network requests without blocking the underlying thread or callbacks.">Introduction to coroutines and channels</a></p></li><li class="list__item" id="-n0i83_28"><p><a href="debug-coroutines-with-idea.html" id="-n0i83_33" data-tooltip="This tutorial demonstrates how to create Kotlin coroutines and debug them using IntelliJ IDEA.">Debug coroutines using IntelliJ IDEA</a></p></li><li class="list__item" id="-n0i83_29"><p><a href="debug-flow-with-idea.html" id="-n0i83_34" data-tooltip="This tutorial demonstrates how to create Kotlin Flow and debug it using IntelliJ IDEA.">Debug Kotlin Flow using IntelliJ IDEA – tutorial</a></p></li><li class="list__item" id="-n0i83_30"><p><a href="https://developer.android.com/kotlin/coroutines/test" id="-n0i83_35" data-external="true" rel="noopener noreferrer">Testing Kotlin coroutines on Android</a></p></li></ul></section></section><section class="chapter"><h2 id="sample-projects" data-toc="sample-projects">Sample projects</h2><ul class="list _bullet" id="-n0i83_36"><li class="list__item" id="-n0i83_37"><p><a href="https://github.com/Kotlin/kotlin-coroutines/tree/master/examples" id="-n0i83_39" data-external="true" rel="noopener noreferrer">kotlinx.coroutines examples and sources</a></p></li><li class="list__item" id="-n0i83_38"><p><a href="https://github.com/JetBrains/kotlinconf-app" id="-n0i83_40" data-external="true" rel="noopener noreferrer">KotlinConf app</a></p></li></ul></section><div class="last-modified">Last modified: 18 October 2022</div><div data-feedback-placeholder="true"></div><div class="navigation-links _bottom"><a href="async-programming.html" class="navigation-links__prev">Asynchronous programming techniques</a><a href="annotations.html" class="navigation-links__next">Annotations</a></div></article><div id="disqus_thread"></div></div></section></main></div><script src="static/v3/app.js"></script></body></html>