Introduction to the
introduce
Coroutines are a concurrent design pattern that you can use on the Android platform to simplify code that executes asynchronously. In simple terms, this is to perform asynchronous tasks.Copy the code
Reference documentation
- Google Developers
- Kotlin document
advantages
- Lightweight: You can run multiple coroutines on a single thread because coroutines support suspension and do not block the thread running them. Suspension saves memory than blocking and supports multiple parallel operations.
- Fewer memory leaks: Multiple operations are performed within a scope using structured concurrency mechanisms.
- Built-in cancel support: Cancel operations are automatically propagated throughout the running coroutine hierarchy.
- Jetpack Integration: Many Jetpack libraries include extensions that provide full coroutine support. Some libraries also provide their own coroutine scope that you can use to structure concurrency.
HelloWorld implementation of coroutines
Depend on the introduction of
implementation 'org. Jetbrains. Kotlinx: kotlinx coroutines -- core: 1.5.0'
implementation 'org. Jetbrains. Kotlinx: kotlinx coroutines - android: 1.5.0'
Copy the code
Code implementation
GlobalScope.launch {
delay(1000)
Log.d(Constants.TAG,"World")
}
Log.d(Constants.TAG,"Hello")
Copy the code
The effect
Hello is printed first, and the coroutine is delayed for one second to output World. The log is printed as follows:
The 2021-08-26 13:50:00. 207, 26316-26316 / demo. Demo. Democoroutines D/Coroutines: Hello 13:50:01. 2021-08-26, 230, 26316-26754 / demo. Demo. Democoroutines D/Coroutines: WorldCopy the code
Structured concurrency of coroutines
Structured concurrency, which sounds a little confusing, is mostly about managing coroutines. If we arbitrarily start coroutines without managing them, we have problems like we don't need them anymore, but we don't reference them or cancel them, and the coroutine is still running, and so on. Structured concurrency is the unified management of coroutine tasks by creating top-level coroutines. Of course, this is a personal understanding, if you have any questions or other views, please point out and discuss.Copy the code
Code implementation
A function is converted to a coroutine primarily through a coroutine builder such as runBlocking, and method calls and regular calls always
private fun test(a)= runBlocking {
val job = GlobalScope.launch {
repeat(100){
delay(1000)
Log.d(Constants.TAG,"Coroutine printing:$it")
}
}
delay(5*1000)
job.cancel()
}
Copy the code
Cancellation of coroutines
Print 100 times (1s delay each time), cancel after 5s.
Code implementation
private fun test(a)= runBlocking {
val job = GlobalScope.launch {
repeat(100){
delay(1000)
Log.d(Constants.TAG,"Coroutine printing:$it")
}
}
delay(5*1000)
job.cancel()
}
Copy the code
The effect
As you can see, it was printed four times, and the task was cancelled when it was printed for the fifth time
The 2021-08-26 14:20:50. 853, 29331-29384 / demo. Demo. Democoroutines D/Coroutines: Coroutines print: 0 2021-08-26 14:20:51. 855, 29331-29384 / demo. The demo. Democoroutines D/Coroutines: Coroutines print: 1 2021-08-26 14:20:52. 858, 29331-29384 / demo. The demo. Democoroutines D/Coroutines: Coroutines printing: 2 2021-08-26 14:20:53. 865, 29331-29384 / demo. The demo. Democoroutines D/Coroutines: Coroutines print: 3Copy the code
The Join of coroutines
Join function
It is mainly to implement occupation execution, and then execute other coroutine tasks after executionCopy the code
Let’s compare the two effects to see the functions of join
Code Implementation 1
private fun test(a)= runBlocking {
GlobalScope.launch {
delay(500)
Log.d(Constants.TAG,"job-test")
}
GlobalScope.launch {
delay(1000)
Log.d(Constants.TAG,"World")
}
Log.d(Constants.TAG,"Hello")}Copy the code
Results 1
Job-test logs are printed as follows:
The 2021-08-26 14:06:08. 607, 27747-27747 / demo. Demo. Democoroutines D/Coroutines: Hello 14:06:09. 2021-08-26, 133, 27747-27807 / demo. Demo. Democoroutines D/Coroutines: Job - test the 2021-08-26 14:06:10. 578, 27747-27807 / demo. The demo. Democoroutines D/Coroutines: WorldCopy the code
Code Implementation 2
private fun test(a)= runBlocking {
val job = GlobalScope.launch {
delay(500)
Log.d(Constants.TAG,"job-test")
}
job.join()
GlobalScope.launch {
delay(1000)
Log.d(Constants.TAG,"World")
}
Log.d(Constants.TAG,"Hello")}Copy the code
The effect
Helloworld is executed after job-test is completed.
The 2021-08-26 14:16:01. 914, 28884-28927 / demo. Demo. Democoroutines D/Coroutines: Job - test the 2021-08-26 14:16:01. 916, 28884-28884 / demo. The demo. Democoroutines D/Coroutines: Hello 14:16:02. 2021-08-26, 919, 28884-28927 / demo. Demo. Democoroutines D/Coroutines: WorldCopy the code
conclusion
This paper mainly introduces the concept, advantages and use of coroutines, and realizes simple coroutine programming, such as HelloWorld, structured programming, cancellation, Join, etc., to record their own learning and understanding of the relevant content, of course, I hope to have a little help or inspiration for everyone, I am very happy. Of course, I also have certain cognitive limitations in recording and understanding in the process of learning and understanding. If you find any problems, please correct them. Thank you.Copy the code