Commit 89f026b 1 parent 3492e3c commit 89f026b Copy full SHA for 89f026b
File tree 3 files changed +137
-1
lines changed
src/main/java/com/kotlin/leran/coroutines
3 files changed +137
-1
lines changed Original file line number Diff line number Diff line change
1
+ * text =auto
2
+ * .css linguist-vendored
3
+ * .scss linguist-vendored
4
+ * .js linguist-vendored
5
+ * .xml linguist-vendored
Original file line number Diff line number Diff line change 52
52
> ` Kotlin ` 是一门` 面向对象 ` 的开发语言。既然是面向对象,则在中级篇主要讲解其` 三大特性 ` ,以及` 类成员 ` 、` 数据类 ` 、` 抽象类 ` 、` 接口类 ` 、` 内部类 ` 、` 继承类 ` 等关于` Kotlin ` 面向对象的方方面面的讲解
53
53
54
54
- [ 1、 类的声明与使用] ( https://juejin.im/post/5a3297de6fb9a045055e295e )
55
- - [ 2、 属性与字段(未完成) ] ( # )
55
+ - [ 2、 属性与字段] ( https://juejin.im/post/5a6378266fb9a01ca10b00e4 )
56
56
- [ 3、 可见性修饰符] ( https://juejin.im/post/5a3293ec51882531926ebfe6 )
57
57
- [ 4、 继承类] ( https://juejin.im/post/5a6303fb51882573467d0fbc )
58
58
- [ 5、 接口类、枚举类] ( https://juejin.im/post/5a34c551518825552b3f9c91 )
Original file line number Diff line number Diff line change
1
+ package com.kotlin.leran.coroutines
2
+
3
+ import kotlinx.coroutines.*
4
+
5
+
6
+ /* *
7
+ * Desc : 协程基础
8
+ * Author : Jetictors
9
+ * Time : 2019/10/9 11:20
10
+
11
+ * Version : v-1.0.1
12
+ */
13
+ class Basis {
14
+
15
+ suspend fun test (){
16
+ first()
17
+ second()
18
+ third()
19
+ fourth()
20
+ five()
21
+ six()
22
+ seven()
23
+ }
24
+
25
+ /* *
26
+ * 第一个协程程序
27
+ */
28
+ private fun first (){
29
+ GlobalScope .launch {
30
+ delay(1000 )
31
+ println (" 协程" )
32
+ }
33
+ println (" kotlin" )
34
+ Thread .sleep(2000 )
35
+ }
36
+
37
+ /* *
38
+ * 用Thread替换协程,达到相同的效果
39
+ */
40
+ private fun second (){
41
+ Thread {
42
+ Thread .sleep(1000 )
43
+ println (" 协程" )
44
+ }.start()
45
+ println (" kotlin" )
46
+ Thread .sleep(2000 )
47
+ }
48
+
49
+ /* *
50
+ * 桥接阻塞
51
+ */
52
+ private fun third (){
53
+ GlobalScope .launch {
54
+ delay(1000 )
55
+ println (" 协程" )
56
+ }
57
+ println (" kotlin" )
58
+ runBlocking {
59
+ delay(2000 )
60
+ }
61
+ }
62
+
63
+ /* *
64
+ * 优化桥接阻塞函数,更符合开发习惯
65
+ */
66
+ private fun fourth () = runBlocking {
67
+ GlobalScope .launch {
68
+ delay(1000 )
69
+ println (" 协程" )
70
+ }
71
+ println (" kotlin" )
72
+ delay(2000 )
73
+ }
74
+
75
+ /* *
76
+ * 等待一个作业
77
+ */
78
+ private suspend fun five (){
79
+
80
+ val job = GlobalScope .launch {
81
+ delay(1000 )
82
+ println (" 协程" )
83
+ }
84
+ println (" kotlin" )
85
+ job.join()
86
+
87
+ }
88
+
89
+ /* *
90
+ * 结构化并发
91
+ */
92
+ private fun six () = runBlocking {
93
+ launch {
94
+ delay(1000 )
95
+ println (" 协程" )
96
+ }
97
+ println (" Kotlin" )
98
+ }
99
+
100
+ /* *
101
+ * 作用域构建器
102
+ */
103
+ private fun seven () = runBlocking {
104
+ launch {
105
+ delay(1000 )
106
+ println (" Task from runBlocking" )
107
+ }
108
+
109
+ coroutineScope{
110
+ launch {
111
+ delay(1500 )
112
+ println (" Task from nested launch" )
113
+ }
114
+
115
+ delay(500 )
116
+ println (" Task from coroutine scope" )
117
+ }
118
+
119
+ println (" Coroutine scope is over" )
120
+ }
121
+
122
+ private fun eight (){
123
+ }
124
+
125
+ private fun nine (){}
126
+
127
+ private fun ten (){
128
+
129
+ }
130
+
131
+ }
You can’t perform that action at this time.
0 commit comments