Home >Backend Development >Golang >An article to introduce you to the basics of Go language concurrency
Go language, specifically for Concurrency# A language born with ##, each time you start a micro-thread the cost of creating one is about 2KB Getting Started
Assume a memory stick size of 4G, a micro-thread 2kb,1G=1024M=1048576kb, 1048576/2=524288
, more than half a million
But do you know how much a thread costs in languages such as Java and Python? ???, 2MB starts with the price directly doubled a thousand times
So, get excited and use it as you like Writing a web program in Go is basically equivalent to Nginx
<br>
Micro-threads in Go are also called goroutine
, goroutine
is the
#Just like I use two hands to operate two mobile phones at the same time to play games
Instead of playing this with one hand and playing that with one hand, this is a switching method
goroutineScheduling is completed by Go’s runtime
,# The essence of ##goroutine
is to switch at the code (user mode) level, the cost is very small
Since <br> Using in Go ##It looped 10 times and took 10s. A bit slow! <br> <br> The function is still the same as above Function Note: Line 6 , with the go keyword added in front, the go keyword means running this function separately in a micro-thread. <br> what??? 0s, what’s going on? <br> <br> This It's because, in Go, we use the daemon thread method. What does that mean? In Go, as long as the main function is executed, other micro-threads will be idle. Just like some monsters, they depend on each other and have a mother. If the mother dies, the baby below will also die. So how to solve this problem??? <br> We found above that some micro-threads were started, but the micro-threads hung up before they had time to execute. This is because main functionIt ran too fast, main finished running, and other micro-threads were automatically closed when Go was running. Then think about it on the other hand, how can we make main at the endWait a moment, wait until my children are all I’m back and I’m continuing to run. 所以,有一个新的问题,那就是等,祭出法宝 先看一下怎么用 可以看到,10个线程同时启动,1s就完了,并且代码相对简单,就算开启10w个,还是1s多一点<br> 这也是为什么很多公司越来越青睐Go的原因。 这个意思要使用多少个核,默认使用全部核心,性能跑满,但是也有意外的情况, 比如一个机器跑了很多其他任务,Go写的这个是不太重要的任务,但是是计算型的,这时候理论来说是不尽量挤兑别人的算力 所以要限制一下当前程序使用电脑的算力 代码 <br> 上述我们学习了Go的并发,学习了 如何创建一个协程(goroutine)。 为什么需要 设置当前程序使用CPU核数。goroutine
is switched by runtime
, Andruntime
After optimization by Google’s digital bosses, it’s already a very small cow going up the mountain, and it’s awesome. Using goroutine
goroutine
is very simple. You just need to add a go before the function you want to call. This is It means starting a goroutineNormal calling function method
Function
func Say() {
time.Sleep(time.Second)
fmt.Println("我在说话说了1s说完了...")
}
main<br>
func main() {
//开始时间
var start_time = time.Now()
//启动10个say说话
for i := 0; i < 10; i++ {
Say()
}
//结束时间
var end_time = time.Now()
//计算时间差
fmt.Println(end_time.Sub(start_time))
}
Execution result<br>
goroutine calling function method
main
func main() {
//开始时间
var start_time = time.Now()
//启动10个say说话
for i := 0; i < 10; i++ {
go Say()
}
//结束时间
var end_time = time.Now()
//计算时间差
fmt.Println(end_time.Sub(start_time))
}
Results of the
Why does this situation of 0s occur
sync .WaitGroup
sync.WaitGroup
函数
func Say() {
//函数结束时取消标记
defer wg.Done()
//每个函数在启动时加上一个标记
wg.Add(1)
//函数开始打上一个标记
time.Sleep(time.Second*1)
fmt.Println("我在说话说了1s说完了...")
}
main<br>
var wg sync.WaitGroup
func main() {
//开始时间
var start_time = time.Now()
//启动10个say说话
for i := 0; i < 10; i++ {
go Say()
}
// 等待所有标记过的微线程执行完毕
wg.Wait()
//结束时间
var end_time = time.Now()
//计算时间差
fmt.Println(end_time.Sub(start_time))
}
执行结果<br>
runtime.GOMAXPROCS<br>
func main() {
//本机的cpu个数
var cpuNum = runtime.NumCPU()
fmt.Println(cpuNum)
//设置Go使用cpu个数
runtime.GOMAXPROCS(4)
}
总结<br>
sync.WaitGroup
。
The above is the detailed content of An article to introduce you to the basics of Go language concurrency. For more information, please follow other related articles on the PHP Chinese website!