-
Notifications
You must be signed in to change notification settings - Fork 7
/
Pool.kt
65 lines (55 loc) · 1.57 KB
/
Pool.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.fish.kotlindemo.coroutinethreadpool
import android.content.Context
import android.widget.Toast
import com.fish.kotlindemo.coroutineraw.launchFish
import kotlinx.coroutines.*
class Pool(val context: Context?) {
fun showStuName() {
GlobalScope.launch(Dispatchers.Main) {
var stuInfo = withContext(Dispatchers.IO) {
//模拟网络请求
Thread.sleep(3000)
"我是小鱼人"
}
//展示
Toast.makeText(context, stuInfo, Toast.LENGTH_SHORT).show()
}
}
fun dealCpuTask() {
GlobalScope.launch(Dispatchers.Main) {
//切换到子线程
withContext(Dispatchers.Default) {
var i = 0
val count = 100000
while(i < count) {
Thread.sleep(1)
}
}
}
}
fun launch() {
GlobalScope.launch(Dispatchers.Default) {
}
}
fun launchIO() {
GlobalScope.launch(Dispatchers.IO) {
println("")
}
}
fun launch3() {
GlobalScope.launch(Dispatchers.IO) {
println("1>>>${Thread.currentThread()}")
withContext(Dispatchers.Default) {
println("2>>>${Thread.currentThread()}")
delay(2000)
println("3>>>${Thread.currentThread()}")
}
println("4>>>${Thread.currentThread()}")
}
}
}
fun main(array: Array<String>) {
var pool = Pool(null)
pool.launchIO()
Thread.sleep(300000)
}