kotlin攜程小記

package com.example.kotlin_demo

import androidx.appcompat.app.AppCompatActivity

import android.os.Bundle

import android.util.Log

import android.widget.ImageView

import android.widget.TextView

import android.widget.Toast

import androidx.lifecycle.Observer

import com.bumptech.glide.Glide

import kotlinx.coroutines.*

import network.LoadState

import kotlinx.android.synthetic.main.activity_main.* //引用xml 控件id作為變量,避免findviewbyid操作

import java.util.concurrent.Flow

/**

* suspend 修飾的函數(shù) 掛起函數(shù),掛起函數(shù)只能在掛起函數(shù)或攜程中運行。函數(shù)掛起掛起的是攜程,而不是攜程所在的線程,掛起后攜程和攜程所在線程脫鉤,掛起結束后回復攜程繼續(xù)執(zhí)行。

*

* 切換線程:withContext 還有其他方法可以切換

*/

/**

*? 修飾符

*? ? 可為空,線程安全性,如果為空不執(zhí)行該表達式

*? !!為空則拋異常

*

*/

class MainActivity : AppCompatActivity() {

? ? var img1:ImageView? = null

? ? var img2:ImageView? = null

? ? var img3:ImageView? = null

? ? private lateinit var mainViewModel : MainModelAndView

? ? override fun onCreate(savedInstanceState: Bundle?) {

? ? ? ? super.onCreate(savedInstanceState)

? ? ? ? setContentView(R.layout.activity_main)

? ? ? ? var tv:TextView = findViewById(R.id.tv)

? ? ? ? var tv2:TextView = findViewById(R.id.tv2)

? ? ? ? var tv3:TextView = findViewById(R.id.tv3)

? ? ? ? var tv4:TextView = findViewById(R.id.tv4)

? ? ? ? img1 = findViewById(R.id.imageView1)

? ? ? ? img2 = findViewById(R.id.imageView2)

? ? ? ? img3 = findViewById(R.id.imageView3)

? ? ? ? /**

? ? ? ? *? 打印結果:

? ? ? ? *? 當前線程start:main

? ? ? ? *? 切換后:DefaultDispatcher-worker-3

? ? ? ? *? 切換后end:main

? ? ? ? */

? ? ? ? GlobalScope.launch(context = Dispatchers.Main,start = CoroutineStart.DEFAULT) {

? ? ? ? ? ? System.out.println("當前線程start:"+ Thread.currentThread().name)

? ? ? ? ? ? myMethod()

? ? ? ? ? ? System.out.println("切換后end:"+ Thread.currentThread().name)

? ? ? ? }

? ? ? ? tv.setOnClickListener {

? ? ? ? ? ? GlobalScope.launch(context = Dispatchers.Main,start = CoroutineStart.DEFAULT) {

? ? ? ? ? ? ? ? System.out.println("----1----")

? ? ? ? ? ? ? ? delay(2000)

? ? ? ? ? ? ? ? System.out.println("----2----")

? ? ? ? ? ? ? ? delay(2000)

? ? ? ? ? ? ? ? System.out.println("----3----")

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? /**

? ? ? ? * test1,test2,test3并發(fā)執(zhí)行 所有執(zhí)行結果出來再相加

? ? ? ? */

? ? ? ? tv2.setOnClickListener {

? ? ? ? ? ? GlobalScope.launch(context = Dispatchers.Default){

? ? ? ? ? ? ? ? val value1 = async { test1() }

? ? ? ? ? ? ? ? val value2 = async { test2() }

? ? ? ? ? ? ? ? val value3 = async { test3() }

? ? ? ? ? ? ? ? all(value1.await(),value2.await(),value3.await())

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? /**

? ? ? ? * est4,test5,test1并發(fā), test4,test5依賴test1的結果

? ? ? ? */

? ? ? ? var job:Job? =null

? ? ? ? tv3.setOnClickListener {

? ? ? ? ? ? ? job = GlobalScope.launch(context = Dispatchers.Default){

? ? ? ? ? ? ? ? val value1 = async { test1() }

? ? ? ? ? ? ? ? val value2 = async { test4(value1.await()) }

? ? ? ? ? ? ? ? val value3 = async { test5(value1.await()) }

? ? ? ? ? ? ? ? all2(value2.await(),value3.await())

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? /**

? ? ? ? * 結束攜程

? ? ? ? */

? ? ? ? tv4.setOnClickListener {

? ? ? ? ? ? if (job?.isActive!!){

? ? ? ? ? ? ? ? job?.cancel()

? ? ? ? ? ? ? ? Log.d("攜程","取消")

? ? ? ? ? ? }else{

? ? ? ? ? ? ? ? Log.d("攜程","已經(jīng)結束")

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? mainViewModel.loadState.observe(this, Observer {

? ? ? ? ? ? when (it){

? ? ? ? ? ? ? ? is LoadState.Success-> button?.isEnabled = true

? ? ? ? ? ? ? ? is LoadState.Loading->button?.isEnabled = false

? ? ? ? ? ? ? ? is LoadState.Fail->{

? ? ? ? ? ? ? ? ? ? button?.isEnabled = true

? ? ? ? ? ? ? ? ? ? Toast.makeText(this,"加載失敗",Toast.LENGTH_SHORT).show()

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? })

? ? ? ? mainViewModel.imageData.observe(this, Observer {

? ? ? ? ? ? Glide.with(this).load(it[0])

? ? ? ? ? ? Glide.with(this).load(it[1])

? ? ? ? ? ? Glide.with(this).load(it[2])

? ? ? ? })

? ? ? ? button.setOnClickListener {

? ? ? ? ? ? mainViewModel.getData()

? ? ? ? }

? ? ? ? canelTv6.setOnClickListener {

? ? ? ? }

? ? }

? ? /**

? ? * 線程切換

? ? */

? ? suspend fun myMethod() {

? ? ? ? withContext(context = Dispatchers.Default){

? ? ? ? ? ? System.out.println("切換后:"+ Thread.currentThread().name)

? ? ? ? }

? ? }

? ? suspend fun test1():Int{

? ? ? ? delay(1000)

? ? ? ? System.out.println("-----執(zhí)行test1-----")

? ? ? ? return? 1

? ? }

? ? suspend fun test2():Int{

? ? ? ? delay(2000)

? ? ? ? System.out.println("-----執(zhí)行test2-----")

? ? ? ? return? 2

? ? }

? ? suspend fun test3():Int{

? ? ? ? delay(1500)

? ? ? ? System.out.println("-----執(zhí)行test3-----")

? ? ? ? return? 3

? ? }

? ? fun all(value1:Int,value2:Int,value3:Int){

? ? ? ? System.out.println("結果:"+ (value1+value2+value3))

? ? }

? ? suspend fun test4(num:Int):Int{

? ? ? ? delay(1500)

? ? ? ? System.out.println("-----執(zhí)行test4-----")

? ? ? ? return? num

? ? }

? ? suspend fun test5(num:Int):Int{

? ? ? ? delay(2000)

? ? ? ? System.out.println("-----執(zhí)行test5-----")

? ? ? ? return? num

? ? }

? ? fun all2(value1:Int,value2:Int){

? ? ? ? System.out.println("結果2:"+ (value1+value2))

? ? }

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容