In 2022, the year of the tiger is full of power~!
foreword
Android
Developers are already familiar with Kotlin
the language , but there may be many students who don't understand itKotlin
.协程
Read most of the articles on the web to get about 协程
a few key words:
- like threads;
- is not a thread;
- user state;
- Collaborative;
Feeling very confused, I 协程
just why there are so many strange and appropriate nouns.
Introduction to coroutines
协程(英語:coroutine)
A class of components of a computer program that promotes cooperative multitasking subroutines that allow execution to be suspended and resumed. Compared with subroutines, coroutines are more general and flexible, but in practice they are not as widely used as subroutines. Coroutines are more suitable for implementing familiar program components, such as cooperative multitasking, exception handling, event loops, iterators, infinite lists, and pipes.
computer physical hardware
协程
Before we talk, let's talk about computer hardware-related knowledge.
Number of physical cpus
Refers to the number of cpu
hardware ( socket
). (However, this concept is often referred to as cpu
a number , which can easily lead to confusion with concepts such as core
numbers, processor
numbers, etc., so the emphasis here is on physical cpu
numbers ).
Since introducing multiple cpu
sockets requires more complex hardware support (connecting different sockets cpu
to memory and other resources), it is usually only done on servers. In a home computer, there is usually only one cpu
slot .
number of cores
At the beginning, each physical cpu
core has only one core a single core
. For the operating system, only one process/thread can run at the same time. In order to improve performance, cpu
manufacturers began to increase cores cpu
on (real hardware exists), and dual-core cpu
( dual-core cpu
) and multi-core cpu
( multiple cores
) appeared. cpu
Such can run two processes/threads at the same time .
Hyper-Threading Technology
Simultaneous multithreading
Hyper-threading technology (hyper-threading/HT)
The essence is the same, it is a technology to increase the number of multi-threads that can be executed core
at the same (make full use of a core
single computing power, try to make it "never idle for a moment").
simultaneous multithreading
The abbreviation is SMT
, AMD
and other names of cpu
manufacturers . hyper–threading
It Intel
is the title of , and can be considered hyper–threading
as a specific technical realization SMT
of .
So it can be said like this: a certain model using SMT
the technology 4核心
AMD cpu
provides the ability to execute 8线程
simultaneously ; a certain model using HT
the technology 2 核心
Intel cpu
provides 4 线程
the ability to execute simultaneously.
Total logical
cpu
number = physicalcpu
number * numbercpu
of * number of hyperthreads per core
Threads and Coroutines
You must never fail to mention it when you 协程
speak 线程
.
线程
It is the smallest unit that the operating system can perform calculations on.
In the general CPU
case, each core can only execute one thread at a time, except that the relatively new ones CPU
have the above-mentioned usage SMT
or HT
technology.
CPU
But 线程
there is no necessary relationship between the number of cores and the number of . To give a very simple example, a piece of my code can always create 100 threads .
CPU
It doesn't understand which instructions it executes 线程
, CPU
and it doesn't need to understand them. It only needs the instructions assigned to it by the current operating system.
In the single-core CPU
era all multithreading is actually multitasking, and multiple tasks are used alternately CPU资源
.
With multi-core, tasks running on two threads can be truly parallelized, but the actual number of cores in the computer will never reach the number of tasks we need for computing. Therefore, the alternate use CPU资源
of has always existed, but we know CPP
that switching the context of the execution thread consumes resources, and the more tasks, the higher the execution efficiency is not necessarily. Some suggestions for computationally intensive programs are to set the optimal number of threads to CPU
Executable Threads 1.5倍
or 1倍+1
.
At this time, we thought about whether we can not CPU
switch , which can reduce the waste of a lot of resources. CPU
Or I/O操作
let other routines execute first during long-term execution to improve resource utilization.
协程
It was at this time that subroutines for cooperative multitasking were created.
At this time, we already 协程
have a preliminary understanding of , let's think back to 协程
the description of the 4 descriptions at the beginning of the article.
- Like threads: During the execution of some programs, the concurrent execution of coroutines is the use of multi-threading technology (for example: no revisions have been made
Java程序
). So it's like a thread; - Not threads: The scheduling of concurrent tasks is not executed through thread switching at the operating system level, but the program itself supports multiple concurrent tasks of a single thread. So it can also be said that they are not threads, they can be called fibers
Fiber
, or green threadsGreenThread
. Just as a process can have multiple threads, a thread can also have multiple coroutines. - User mode: It is not a system-level thread and can execute asynchronous tasks autonomously. This kind of lightweight thread managed by the programmer's own program is called a user space thread , which has the characteristics of being invisible to the kernel.
- Collaborative: Each running program is required to position itself to give up its execution rights, so that multiple tasks can be executed alternately. Wikipedia: Cooperative multitasking ;
Coroutines in Android
The above mentioned 协程
reduces context switching and improves efficiency, so does Android
it kotlin
support coroutines?
kotlin
The official document says: Essentially, coroutines are lightweight threads.
But Kotlin-JVM
for 协程
it's threads. It is essentially a package based on Java Thread API
native .
Maybe Kotlin
subsequent versions will have real coroutine-related mechanisms instead of threads.
At this time, we may have some doubts. 协程
Since Android
there is still 线程
no , Java
and both in Executor
and Android
in AsyncTask
can provide concurrent tasks, then what is the use kotlin
of 协程
it ? There will be an article explaining it separately~!
References:
Read an article to understand what are processes, threads, and coroutines
Are Kotlin coroutines really more efficient than Java threads?
Throwing the Line: A Glimpse of Coroutine Use Cases in Kotlin
The article is all told here, if you have other needs to communicate, you can leave a message~! ~!
If you want to read more articles by the author, you can check out my personal blog and public account: