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 修饰的函数 挂起函数,挂起函数只能在挂起函数或携程中运行。函数挂起挂起的是携程,而不是携程所在的线程,挂起后携程和携程所在线程脱钩,挂起结束后回复携程继续执行。
*
* 切换线程:withContext 还有其他方法可以切换
*/
/**
* 修饰符
* ? 可为空,线程安全性,如果为空不执行该表达式
* !!为空则抛异常
*
*/
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并发执行 所有执行结果出来再相加
*/
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并发, 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("携程","已经结束")
}
}
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("-----执行test1-----")
return 1
}
suspend fun test2():Int{
delay(2000)
System.out.println("-----执行test2-----")
return 2
}
suspend fun test3():Int{
delay(1500)
System.out.println("-----执行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("-----执行test4-----")
return num
}
suspend fun test5(num:Int):Int{
delay(2000)
System.out.println("-----执行test5-----")
return num
}
fun all2(value1:Int,value2:Int){
System.out.println("结果2:"+ (value1+value2))
}
}