协程实战

添加对协程的依赖

  //协程依赖
  implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.5'
  implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.5'

 

添加对kotlin的依赖

 //Kotlin依赖
 implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
 implementation 'androidx.core:core-ktx:1.2.0'

添加对KTX 的依赖

 //对KTX的支持 livedata、viewmodel
 implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.2.0'
 implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.2.0'
 implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'

MainActivity.kt

package com.zhoujian.coroutinesdemo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.*

class MainActivity : AppCompatActivity() {

    private val scope = MainScope()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //需要手动在onDestroy方法中取消
//        scope.launch(Dispatchers.Main) {
//            io1()
//            main1()
//            io2()
//            main2()
//            io3()
//            main3()
//        }

        //lifecycleScope 利用了KTX,onDestroy会帮助我们取消,防止协程泄漏
        lifecycleScope.launch(Dispatchers.Main) {
            io1()
            main1()
            io2()
            main2()
            io3()
            main3()
        }
    }

    private suspend fun method() {
        withContext(Dispatchers.IO) {
            println("在后台开启协程,method() in thread---${Thread.currentThread().name}")
        }
    }

    private suspend fun io1() {
        withContext(Dispatchers.IO) {
            Thread.sleep(2000)
            println("io1() in thread---${Thread.currentThread().name}")
        }
    }

    private suspend fun io2() {
        withContext(Dispatchers.IO) {
            Thread.sleep(2000)
            println("io2() in thread---${Thread.currentThread().name}")
        }
    }

    private suspend fun io3() {
        withContext(Dispatchers.IO) {
            Thread.sleep(2000)
            println("io3() in thread---${Thread.currentThread().name}")
        }
    }

    private fun main1() {
        println("main1() in thread---${Thread.currentThread().name}")
    }

    private fun main2() {
        println("main2() in thread---${Thread.currentThread().name}")
    }

    private fun main3() {
        println("main3() in thread---${Thread.currentThread().name}")
    }

    override fun onDestroy() {
        //手动取消,防止协程泄漏
        scope?.cancel()
        super.onDestroy()
    }
}

 

打印结果:

04-25 10:16:53.311 22375-22415/com.zhoujian.coroutinesdemo I/System.out: io1() in thread---DefaultDispatcher-worker-2
04-25 10:16:53.311 22375-22375/com.zhoujian.coroutinesdemo I/System.out: main1() in thread---main
04-25 10:16:55.313 22375-22415/com.zhoujian.coroutinesdemo I/System.out: io2() in thread---DefaultDispatcher-worker-2
04-25 10:16:55.315 22375-22375/com.zhoujian.coroutinesdemo I/System.out: main2() in thread---main
04-25 10:16:57.316 22375-22417/com.zhoujian.coroutinesdemo I/System.out: io3() in thread---DefaultDispatcher-worker-3
04-25 10:16:57.317 22375-22375/com.zhoujian.coroutinesdemo I/System.out: main3() in thread---main

 

你可能感兴趣的:(Kotlin实战)