process
A process is a process of execution of a program in the operating system. It is the basic unit for the system to allocate and schedule resources. A process is a dynamic concept and the basic unit for the program to allocate and manage resources during the execution process. Each process has its own address space. A process has at least 5 basic states: initial state, execution state, waiting state, ready state, and terminating state. In layman's terms, a process is an executing program.
Thread
A thread is an execution instance of a process and is the smallest unit of program execution. It is a smaller basic unit that can run independently than a process. A process can create multiple threads, multiple threads in the same process can be executed concurrently, and if a program is to be run, there will be at least one process.
concurrent
Concurrency refers to processing multiple tasks within the same time period, and switching between multiple tasks makes it appear to be carried out simultaneously on the surface. In Go, concurrent programming can be implemented using goroutine and channel.
Features:
Multiple tasks act on one CPU
Only one task can be executed at the same time
Execute multiple tasks within the same time period
parallel
Parallelism refers to processing multiple tasks simultaneously, that is, multiple tasks are executed on different processors at the same time. Parallelism can significantly improve program performance, especially in multi-core CPUs, where multiple CPU cores can be used for computing. In Go, parallel programming can be implemented using the GOMAXPROCS functions of the goroutine and runtime packages.
Features:
Multiple tasks act on multiple CPUs
Execute multiple tasks at the same time
In layman's terms, multi-threaded programs run on a single-core CPU are concurrent, and multi-threaded programs run on a multi-core CUP are parallel. If the number of threads is greater than the number of CPU cores, then multi-threaded programs run on multiple CPUs are both parallel and concurrent.
coroutine
In the Go language, goroutines are lightweight threads, which are the basis for implementing concurrent programming in the Go language. Compared with traditional threads, coroutine creation and switching are very lightweight, and can create thousands of coroutines within a single thread, and the switching overhead is very small, so efficient concurrent programming can be achieved.
Coroutines in the Go language are managed and scheduled by the Go runtime scheduler. When the program starts, the Go runtime will start a main coroutine by default. The main coroutine will create other child coroutines, which will be assigned to different system threads for execution. When a coroutine blocks, the Go runtime suspends the coroutine and leaves the CPU, and executes other coroutines to make full use of system resources.
In Go, creating coroutines is very simple, you only need to add the go keyword before the function call.
For example:
func main() { go func() { ("Hello, world!") }() }
Use waiting for the coroutine to complete execution
func test1() { for i := 0; i < 10; i++ { //Output every 100 milliseconds ("test1() HelloGolang-", i) ( * 100) } () //Add to -1 by coroutine counter } func main() { //Notice: //1. After the main thread is executed, the program will exit even if the coroutine does not complete the execution. //2. The coroutine can exit in advance before the main thread is executed. Whether the coroutine is executed will not affect the execution of the main thread. //In order to ensure that our program can be executed smoothly, we want the coroutine to execute before the main process exits. //At this time we can use the waiting coroutine to complete execution (1) //The coroutine counter is increased by 1 go test1() // means to open a coroutine for i := 0; i < 10; i++ { //Output every 50 milliseconds ("mian() HelloGolang-", i) ( * 50) } () //Waiting for the coroutine to complete execution ("The main thread exits....") }
Multi-coroutines and multi-threaded
Each goroutine (coroutine) in Golang takes up less memory by default than Java and C threads. OS threads (operating system threads) generally have fixed stack memory (usually about 2MB). A goroutine (coroutine) takes up very little memory, only about 2KB. The overhead of multi-coroutine switching scheduling is much less than that of threads. This is one of the reasons why more and more large companies use Golang.
func test1() { for i := 0; i < 10; i++ { //Output every 100 milliseconds ("test1() HelloGolang-", i) ( * 100) } () //Add to -1 by coroutine counter } func test2() { for i := 0; i < 10; i++ { //Output every 100 milliseconds ("test2() HelloGolang-", i) ( * 100) } () //Add to -1 by coroutine counter } func main() { (1) //The coroutine counter is increased by 1 go test1() // means to open a coroutine (1) //The coroutine counter is increased by 1 go test2() // means to open a coroutine () ("The main program exits....") }
The above is a detailed analysis of the goroutine in Golang. For more information about Golang coroutine, please follow my other related articles!