You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
**Kotlin coroutines are a powerful tool for managing background tasks, making asynchronous programming easier and more efficient**. This comprehensive guide covers all aspects of concurrent programming in Kotlin.
1459
1493
1460
-
### 4.1. Introduction to Coroutines
1494
+
### Introduction to Coroutines
1461
1495
1462
1496
#### What are Coroutines?
1463
1497
@@ -1471,9 +1505,9 @@ fun main() {
1471
1505
-**Built-in cancellation support**: **Cancellation is propagated automatically through the running coroutine hierarchy**
1472
1506
-**Fewer memory leaks**: **Use structured concurrency to run operations within a scope**
1473
1507
1474
-
### 4.2. Coroutine Builders
1508
+
### Coroutine Builders
1475
1509
1476
-
#### 4.2.1 Launch - Fire and Forget
1510
+
#### Launch - Fire and Forget
1477
1511
1478
1512
::: kotlin-playground Launch Example
1479
1513
@@ -1498,7 +1532,7 @@ fun main() = runBlocking {
1498
1532
- Used for tasks that don't return a result
1499
1533
- Fire-and-forget operations
1500
1534
1501
-
#### 4.2.2 Async - Result-Oriented
1535
+
#### Async - Result-Oriented
1502
1536
1503
1537
**Launches a coroutine that returns a result asynchronously. Returns a `Deferred<T>`, which is like a Future in Java** :
1504
1538
@@ -1524,7 +1558,7 @@ fun main() = runBlocking {
1524
1558
```
1525
1559
:::
1526
1560
1527
-
#### 4.2.3 RunBlocking - Bridging Blocking and Non-blocking
1561
+
#### RunBlocking - Bridging Blocking and Non-blocking
1528
1562
1529
1563
::: kotlin-playground RunBlocking Example
1530
1564
@@ -1547,11 +1581,11 @@ fun main() = runBlocking {
1547
1581
```
1548
1582
:::
1549
1583
1550
-
### 4.3. Dispatchers - Thread Management
1584
+
### Dispatchers - Thread Management
1551
1585
1552
1586
**Dispatchers control where and how your coroutines run, like assigning them to specific threads or pools** .
1553
1587
1554
-
#### 4.3.1 Types of Dispatchers
1588
+
#### Types of Dispatchers
1555
1589
1556
1590
::: kotlin-playground Dispatchers Example
1557
1591
@@ -1583,7 +1617,7 @@ fun main() = runBlocking {
1583
1617
```
1584
1618
:::
1585
1619
1586
-
#### 4.3.2 WithContext - Switching Context
1620
+
#### WithContext - Switching Context
1587
1621
1588
1622
**withContext() calls the given code with the specified coroutine context, is suspended until it completes, and returns the result** :
1589
1623
@@ -1621,11 +1655,11 @@ fun main() = runBlocking {
1621
1655
```
1622
1656
:::
1623
1657
1624
-
### 4.4. Structured Concurrency
1658
+
### Structured Concurrency
1625
1659
1626
1660
**The CoroutineScope is the foundation of structured concurrency. It serves as a container for coroutines, defining the scope within which coroutines are launched** .
1627
1661
1628
-
#### 4.4.1 CoroutineScope
1662
+
#### CoroutineScope
1629
1663
::: kotlin-playground CoroutineScope Example
1630
1664
1631
1665
@file main.kt
@@ -1653,7 +1687,7 @@ fun main() = runBlocking {
1653
1687
```
1654
1688
:::
1655
1689
1656
-
#### 4.4.2 Structured Hierarchy
1690
+
#### Structured Hierarchy
1657
1691
1658
1692
**With structured concurrency, you can specify the major context elements (like dispatcher) once, when creating the top-level coroutine. All the nested coroutines then inherit the context and modify it only if needed** :
1659
1693
@@ -1694,7 +1728,7 @@ fun main() = runBlocking {
1694
1728
```
1695
1729
:::
1696
1730
1697
-
#### 4.4.3 Supervision
1731
+
#### Supervision
1698
1732
1699
1733
::: kotlin-playground Supervision Example
1700
1734
@@ -1731,11 +1765,11 @@ fun main() = runBlocking {
1731
1765
```
1732
1766
:::
1733
1767
1734
-
### 4.5. Channels - Communication Between Coroutines
1768
+
### Channels - Communication Between Coroutines
1735
1769
1736
1770
**Channels provide a way to share information between different coroutines** .
1737
1771
1738
-
#### 4.5.1 Basic Channel Usage
1772
+
#### Basic Channel Usage
1739
1773
1740
1774
::: kotlin-playground Basic Channel Example
1741
1775
@@ -1768,7 +1802,7 @@ fun main() = runBlocking {
1768
1802
:::
1769
1803
1770
1804
1771
-
#### 4.5.2 Channel Types
1805
+
#### Channel Types
1772
1806
1773
1807
**By default, a "Rendezvous" channel is created** :
1774
1808
@@ -1815,7 +1849,7 @@ fun main() {
1815
1849
}
1816
1850
```
1817
1851
:::
1818
-
#### 4.5.3 Producer Pattern
1852
+
#### Producer Pattern
1819
1853
1820
1854
::: kotlin-playground Producer Pattern Example
1821
1855
@@ -1848,11 +1882,11 @@ fun main() {
1848
1882
```
1849
1883
:::
1850
1884
1851
-
### 4.6. Flow - Reactive Streams
1885
+
### Flow - Reactive Streams
1852
1886
1853
1887
**Think of Flows as streams of data flowing through your server-side code, like the continuous stream of orders coming from the tables** .
1854
1888
1855
-
#### 4.6.1 Basic Flow
1889
+
#### Basic Flow
1856
1890
1857
1891
::: kotlin-playground Basic Flow Example
1858
1892
@@ -1877,7 +1911,7 @@ fun main() = runBlocking {
1877
1911
```
1878
1912
:::
1879
1913
1880
-
#### 4.6.2 Flow Operators
1914
+
#### Flow Operators
1881
1915
1882
1916
::: kotlin-playground Flow Operators Example
1883
1917
@@ -1896,7 +1930,7 @@ fun main() = runBlocking {
1896
1930
```
1897
1931
:::
1898
1932
1899
-
#### 4.6.3 Cold vs Hot Flows
1933
+
#### Cold vs Hot Flows
1900
1934
1901
1935
::: kotlin-playground Cold vs Hot Flows Example
1902
1936
@@ -1945,11 +1979,11 @@ fun main() {
1945
1979
:::
1946
1980
1947
1981
1948
-
### 4.7. Exception Handling
1982
+
### Exception Handling
1949
1983
1950
1984
**Structured concurrency enhances error handling by propagating exceptions up to the nearest exception handler in the coroutine hierarchy** .
1951
1985
1952
-
#### 4.7.1 Try-Catch in Coroutines
1986
+
#### Try-Catch in Coroutines
1953
1987
1954
1988
::: kotlin-playground Try-Catch Example
1955
1989
@@ -1976,7 +2010,7 @@ fun main() = runBlocking {
1976
2010
```
1977
2011
:::
1978
2012
1979
-
#### 4.7.2 CoroutineExceptionHandler
2013
+
#### CoroutineExceptionHandler
1980
2014
1981
2015
::: kotlin-playground Exception Handler Example
1982
2016
@@ -2001,9 +2035,9 @@ fun main() = runBlocking {
2001
2035
```
2002
2036
:::
2003
2037
2004
-
### 4.8. Cancellation and Timeout
2038
+
### Cancellation and Timeout
2005
2039
2006
-
#### 4.8.1 Cooperative Cancellation
2040
+
#### Cooperative Cancellation
2007
2041
2008
2042
::: kotlin-playground Cancellation Example
2009
2043
@@ -2034,7 +2068,7 @@ fun main() = runBlocking {
2034
2068
```
2035
2069
:::
2036
2070
2037
-
#### 4.8.2 Timeout
2071
+
#### Timeout
2038
2072
2039
2073
::: kotlin-playground Timeout Example
2040
2074
@@ -2058,11 +2092,11 @@ fun main() = runBlocking {
2058
2092
```
2059
2093
:::
2060
2094
2061
-
### 4.9. Thread Safety and Synchronization
2095
+
### Thread Safety and Synchronization
2062
2096
2063
2097
**One approach to addressing shared mutable state is by using thread-safe data structures provided by the Kotlin standard library, such as Atomic types** .
0 commit comments