当前位置:网站首页>goroutine (coroutine) in go language
goroutine (coroutine) in go language
2022-08-02 06:11:00 【m0_67400972】
Article Directory
goroutine(coroutine)
1. Process and thread description:
- A process is an execution process of a program in the operating system, and it is the basic unit of resource allocation and scheduling in the system.
- A thread is an execution instance of a process and the smallest unit of program execution. It is a basic unit smaller than a process that can run independently.
- A process can create and destroy multiple threads, and multiple threads in the same process can execute concurrent.
- A program has at least one process, and a process has at least one thread.
2. Description of concurrency and parallelism:
- Multi-threaded programs running on a single core are concurrent.
- Multi-threaded programs run on multiple cores, which is parallelism.
3.go coroutine and go main thread:
- Go main thread (some programmers directly call it a thread/can also be understood as a process): On a go thread, multiple coroutines can be started.It can be understood as: coroutines are lightweight threads (optimized by the compiler).
- Features of go coroutines:
1) Independent stack space
2) Shared program heap space
3) Scheduling is controlled by the user
4) Coroutines are lightweight threads
goroutine-quick start case
Case description:
Write a program to complete the following functions:
1) In the main thread (which can be understood as a process), start agoroutine, this coroutine outputs "hello, world" every second
2) The main thread also outputs "hello, golang" every second, after 10 outputs, exit the program
3) Request the main threadThreads and goroutines execute simultaneously
package mainimport ("fmt""strconv""time")//Write a function that prints "hello,world" every secondfunc test() {for i:=1;i<=10;i++ {fmt.Println("test hello,world "+strconv.Itoa(i))time.Sleep(time.Second)}}func main() {go test() //Open a coroutine to execute it at the same timefor i:=1;i<=10;i++ {fmt.Println("mian() hello,world "+strconv.Itoa(i))time.Sleep(time.Second)}}
Summary:
- The main thread is a physical thread that acts directly on the CPU.It is heavyweight and consumes a lot of CPU resources.
- Coroutines are started from the main thread, which are lightweight threads and logical states.Relatively small consumption of resources.
- Golang's coroutine mechanism is an important feature, which can easily open tens of thousands of coroutines.The concurrency mechanism of other programming languages is generally based on threads. Too many threads are opened, and the resource consumption is high. This highlights the advantages of Golang in concurrency.
4. Basic introduction to MPG mode
M: The main thread of the operating system (the physical thread)
P: The context required for the execution of the coroutine
G: The coroutine
5. Set the number of CPUs running in golang
package mainimport "fmt"import "runtime"func main() {//Get the number of CPUs in the current systemnum := runtime.NumCPU()//Set the cpu of num-1 to run the go program hereruntime.GOMAXPROCS(num)fmt.Println("num=",num)}//output result: num = 16
Note: After go1.8, the program runs on multiple cores by default, so it is not necessary to set it.Before go1.8, it needs to be set.
Let me introduce myself first. The editor graduated from Shanghai Jiaotong University in 2013. I worked in a small company and went to big factories such as Huawei and OPPO. I joined Alibaba in 2018, until now.I know that most junior and intermediate java engineers want to upgrade their skills, they often need to explore their own growth or sign up to study, but for training institutions, the tuition fee is nearly 10,000 yuan, which is really stressful.Self-learning that is not systematic is very inefficient and lengthy, and it is easy to hit the ceiling and the technology stops.Therefore, I collected a "full set of learning materials for java development" for everyone. The original intention is also very simple. I hope to help friends who want to learn by themselves but don't know where to start, and at the same time reduce everyone's burden.Add the business card below to get a full set of learning materials
边栏推荐
猜你喜欢
MySQL 5.7详细下载安装配置教程
[email protected](使用passwordYES)"/>
Navicat报错:1045 -拒绝访问用户[email protected](使用passwordYES)
H5接入支付流程-微信支付&支付宝支付
The company does not pay attention to software testing, and the new Ali P8 has written a test case writing specification for us
Android studio connects to MySQL and completes simple login and registration functions
简道云-灵活易用的应用搭建平台
MySQL implements sorting according to custom (specified order)
【HCIE】NO.45 Hub and Spoke配置案例
navicat新建数据库
mysql安装教程【安装版】
随机推荐
ORA-04044:此处不允许过程、函数、程序包或类型,系统分析与解决
What do interview test engineers usually ask?The test supervisor tells you
Introduction and use of apifox (1).
prisma使用mongodb副本集群报错引发的一些列问题
Detailed explanation of AMQP protocol
Android studio连接MySQL并完成简单的登录注册功能
go项目的打包部署
matlab simulink 飞机飞行状态控制
MySQL multi-table association one-to-many query to get the latest data
How much does a test environment cost? Start with cost and efficiency
MySQL 8.0.29 设置和修改默认密码
Mysql常用命令大全
Android studio connects to MySQL and completes simple login and registration functions
[QNX Hypervisor 2.2用户手册]9.18 unsupported
【Gazebo入门教程】第一讲 Gazebo的安装、UI界面、SDF文件介绍
Mysql implements optimistic locking
Grid布局介绍
【HCIE】NO.45 Hub and Spoke配置案例
一线大厂软件测试流程(思维导图)详解
浏览器的onload事件