当前位置:网站首页>Introduction to kotlin collaboration
Introduction to kotlin collaboration
2022-07-03 01:40:00 【AnRFDev】
development environment
- IntelliJ IDEA 2021.2.2 (Community Edition)
- Kotlin: 212-1.5.10-release-IJ5284.40
Introduce Kotlin Association in . Use an example to show the basic usage of coprocessing .
First example
New project
We use the community version IntelliJ IDEA 2021.2.2. Create a new one Kotlin Engineering is used to test .
Lead in process
The project with gradle Conduct management , We are Github Find the dependency of the synergy on .
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2")
}
- 1.
- 2.
- 3.
Project modification gradle To configure . Add dependencies .
Code example
Create a class and then main
Method to write the relevant code of the coroutine .
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
fun main() {
GlobalScope.launch { // Start a new process in the background and continue
delay(300) // wait for 300 millisecond
"rustfisher.com".forEach {
print(it)
delay(200) // Wait every time you print
}
}
println("RustFisher")
Thread.sleep(3000) // Blocking the main thread prevents too fast exit
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
Code run results
Essentially , A coroutine is a lightweight thread .
We use it GlobalScope Started a new collaborative process , This means that the life cycle of the new collaboration is limited only by the life cycle of the entire application .
Can be GlobalScope.launch { …… }
Replace with thread { …… }
, And will delay(……)
Replace with Thread.sleep(……)
Achieve the same purpose . Pay attention to the import package kotlin.concurrent.thread
Replace a coroutine with a thread
import java.lang.Thread.sleep
import kotlin.concurrent.thread
fun main() {
thread {
sleep(300)
"rustfisher.com".forEach {
print(it)
sleep(200)
}
}
println("RustFisher")
sleep(3000) // Blocking the main thread prevents too fast exit
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
If thread{}
contains delay
, The compiler will report an error
Suspend function 'delay' should be called only from a coroutine or another suspend function
because delay
It's a special one Suspend function , It doesn't block threads , But will Hang up coroutines , And it can only be used in the cooperation process .
thus , To summarize the first collaborative process example
- gradle Lead in process
kotlinx-coroutines-core
-
GlobalScope.launch
Start the coroutines - In the process of cooperation Hang up function
delay(long)
It can achieve the effect of delay , And it can only be used in collaboration
The thread where the coroutine is located
Essentially, a coroutine is a lightweight thread . Let's look at the thread information where the coroutine is located .
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.lang.Thread.sleep
fun main() {
println("main Thread information ${Thread.currentThread().id}")
for (i in 1..3) { // Start the process several times
GlobalScope.launch {
println(" Start the process #$i Thread id: ${Thread.currentThread().id}")
}
}
sleep(2000) // Blocking the main thread prevents too fast exit
println("RustFisher The example ends ")
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
function log as follows
main Thread information 1
Start the process #1 Thread id: 11
Start the process #3 Thread id: 14
Start the process #2 Thread id: 13
RustFisher The example ends
- 1.
- 2.
- 3.
- 4.
- 5.
You can see that the process is started many times , These coroutines are not necessarily in the same thread . In other words, there are Same thread The possibility of .
So try to start the process crazily , Increase the number of cycles for (i in 1..3000)
. Observe log It can be seen that , There are duplicate threads id.
Records of a software engineer
边栏推荐
- CF1617B Madoka and the Elegant Gift、CF1654C Alice and the Cake、 CF1696C Fishingprince Plays With Arr
- 【数据挖掘】任务3:决策树分类
- [technology development-23]: application of DSP in future converged networks
- 【数据挖掘】任务4:20Newsgroups聚类
- [self management] time, energy and habit management
- MySQL - database query - condition query
- String splicing function of MySQL
- Openresty cache
- Related concepts of GDB in embedded system
- Everything文件搜索工具
猜你喜欢
Using tensorboard to visualize the model, data and training process
Wireshark data analysis and forensics a.pacapng
How is the mask effect achieved in the LPL ban/pick selection stage?
[data mining] task 3: decision tree classification
QTableWidget懒加载剩内存,不卡!
leetcode刷题_两数之和 II - 输入有序数组
Using tensorboard to visualize the model, data and training process
C#应用程序界面开发基础——窗体控制(3)——文件类控件
C application interface development foundation - form control (2) - MDI form
High resolution network (Part 1): Principle Analysis
随机推荐
什么是调。调的故事
The difference between tail -f, tail -f and tail
What are the trading forms of spot gold and what are the profitable advantages?
Related concepts of GDB in embedded system
Expérience de recherche d'emploi d'un programmeur difficile
并发编程的三大核心问题 -《深入理解高并发编程》
Wordinsert formula /endnote
[FPGA tutorial case 6] design and implementation of dual port RAM based on vivado core
C#应用程序界面开发基础——窗体控制(2)——MDI窗体
Take you ten days to easily complete the go micro service series (I)
Look at how clothing enterprises take advantage of the epidemic
Tp6 fast installation uses mongodb to add, delete, modify and check
网络安全-钓鱼
Common English Vocabulary
[keil5 debugging] debug is stuck in reset_ Handler solution
网络安全-DNS欺骗与钓鱼网站
C application interface development foundation - form control (2) - MDI form
Force buckle 204 Count prime
一位苦逼程序员的找工作经历
Summary of interval knowledge