Kotlin协程实战:比Java线程更优雅的并发编程
<h2>前言</h2><p>Kotlin 协程(Coroutines)是 Android 和服务端开发者的必备技能。相比 Java 线程,协程更轻量、更易读、更安全。本文带你从零掌握 Kotlin 协程的核心用法。</p>
<h2>一、协程 vs 线程</h2>
<ul>
<li><strong>线程</strong>:操作系统调度,创建成本高(约1MB栈内存)</li>
<li><strong>协程</strong>:用户态调度,创建成本极低(约几KB),可创建数十万个</li>
</ul>
<pre><code>// Java 线程:10000个线程会OOM
for (int i = 0; i < 10000; i++) {
new Thread(() -> doWork()).start();
}
// Kotlin 协程:100000个协程轻松运行
repeat(100_000) {
GlobalScope.launch { doWork() }
}</code></pre>
<h2>二、第一个协程</h2>
<pre><code>import kotlinx.coroutines.*
fun main() = runBlocking {
// 启动协程
launch {
delay(1000)// 非阻塞延迟
println("World!")
}
println("Hello,")
}
// 输出:
// Hello,
// (1秒后) World!</code></pre>
<h2>三、协程作用域(CoroutineScope)</h2>
<pre><code>// 1. GlobalScope:全局作用域,生命周期跟随应用
GlobalScope.launch {
println("Global scope")
}
// 2. runBlocking:阻塞当前线程直到协程完成
runBlocking {
launch { println("runBlocking") }
}
// 3. coroutineScope:挂起当前协程,等待子协程完成
suspend fun loadData() = coroutineScope {
launch { fetchUser() }
launch { fetchOrders() }
}
// 4. ViewModelScope(Android):跟随 ViewModel 生命周期
viewModelScope.launch {
val data = repository.fetchData()
updateUI(data)
}</code></pre>
<h2>四、挂起函数(suspend)</h2>
<pre><code>// 挂起函数:可在协程中暂停执行,不阻塞线程
suspend fun fetchUser(): User {
return withContext(Dispatchers.IO) {
// 切换到 IO 线程执行网络请求
api.getUser()
}
}
// 调用挂起函数
fun loadData() = viewModelScope.launch {
showLoading()
val user = fetchUser()// 挂起,等待结果
updateUI(user) // 恢复执行
hideLoading()
}</code></pre>
<h2>五、协程调度器(Dispatcher)</h2>
<pre><code>// 1. Dispatchers.Main:主线程(UI 操作)
withContext(Dispatchers.Main) {
textView.text = "Updated"
}
// 2. Dispatchers.IO:IO 密集型(网络、数据库)
withContext(Dispatchers.IO) {
val data = api.fetchData()
database.save(data)
}
// 3. Dispatchers.Default:CPU 密集型(排序、计算)
withContext(Dispatchers.Default) {
val sorted = list.sortedBy { it.id }
}
// 4. 自定义线程池
val customDispatcher = Executors.newFixedThreadPool(4).asCoroutineDispatcher()
withContext(customDispatcher) {
heavyComputation()
}</code></pre>
<h2>六、异常处理</h2>
<pre><code>// CoroutineExceptionHandler:全局异常捕获
val handler = CoroutineExceptionHandler { _, e ->
println("Caught: $e")
}
val job = GlobalScope.launch(handler) {
throw RuntimeException("Error!")
}
// try-catch:局部异常处理
try {
val result = async {
riskyOperation()
}.await()
} catch (e: Exception) {
handleError(e)
}</code></pre>
<h2>七、实战:并发请求</h2>
<pre><code>data class User(val id: Int, val name: String)
data class Order(val id: Int, val userId: Int)
suspend fun loadUserData(userId: Int): Pair<User, List<Order>> {
return coroutineScope {
// 并发执行两个请求
val userDeferred = async { fetchUser(userId) }
val ordersDeferred = async { fetchOrders(userId) }
// 等待结果
Pair(userDeferred.await(), ordersDeferred.await())
}
}
// 对比 Java CompletableFuture
// Kotlin 协程写法更简洁,无需回调链</code></pre>
<h2>总结</h2>
<p>Kotlin 协程让并发编程变得优雅。核心要点:</p>
<ul>
<ul>
<li><strong>轻量级</strong>:可创建数十万协程而不 OOM</li>
</ul>
</ul>
<strong>结构化并发</strong>
<ul>
<ul>:作用域自动管理协程生命周期</ul>
</ul>
<strong>挂起函数</strong>
<ul>
<ul>:用同步写法实现异步逻辑</ul>
</ul>
<strong>调度器</strong>
<ul>:灵活切换线程,适配不同场景</ul>
<p>觉得有帮助请点赞收藏!有问题欢迎评论区交流 🚀</p>
</div>
<div id="MySignature" role="contentinfo">
---
📌 **如果觉得文章对你有帮助,欢迎点赞👍收藏⭐!**
💬 有问题或建议?欢迎在评论区留言讨论~
🔗 更多技术干货请关注作者:弥烟袅绕
📚 本文地址:https://www.cnblogs.com/czlws/p/19820642/kotlin-coroutines-concurrency-async<br><br>
来源:https://www.cnblogs.com/czlws/p/19820642/kotlin-coroutines-concurrency-async
頁:
[1]