当前位置:网站首页>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
边栏推荐
- 2021年软件测试面试题大全
- [QNX Hypervisor 2.2用户手册]9.19 user
- Google Chrome(谷歌浏览器)安装使用
- MySQL如何对SQL做prepare预处理(解决IN查询SQL预处理仅能查询出一条记录的问题)
- H5如何实现唤起APP
- Navicat cannot connect to mysql super detailed processing method
- AMQP协议详解
- Navicat报错:1045 -拒绝访问用户[email protected](使用passwordYES)
- Matlab学习第二天
- ORA-04044:此处不允许过程、函数、程序包或类型,系统分析与解决
猜你喜欢
100 latest software testing interview questions in 2022, summary of common interview questions and answers
matlab simulink 模糊pid结合smith控制温度
Android studio connects to MySQL and completes simple login and registration functions
MySQL 8.0.29 set and modify the default password
MySQL 游标
公司不重视软件测试,新来的阿里P8给我们撰写了测试用例编写规范
【HCIE】NO.45 Hub and Spoke配置案例
matlab simulink 飞机飞行状态控制
MySQL(7)
12 reasons for MySQL slow query
随机推荐
18年程序员生涯,读了200多本编程书,挑出一些精华分享给大家
How to quickly delete the compressed package password?
MySQL 游标
Mysql 回表
12 reasons for MySQL slow query
Matlab论文插图绘制模板第41期—气泡图(bubblechart)
leetcode 665. Non-decreasing Array 非递减数列(中等)
11种你需要了解的物联网(IoT)协议
MySql copies data from one table to another table
【MLT】MLT多媒体框架生产消费架构解析(一)
MYSQL 唯一约束
navicat新建数据库
行测不会概念
[PSQL] 窗口函数、GROUPING运算符
MySQL 字符串拼接 - 多种字符串拼接实战案例
2022年100道最新软件测试面试题,常见面试题及答案汇总
Mysql实现乐观锁
golang's time package: methods for time interval formatting and output of timestamp formats such as seconds, milliseconds, and nanoseconds
复盘:图像饱和度计算公式和图像信噪(PSNR)比计算公式
2022年7月学习计划完成情况