当前位置:网站首页>Runtime package for golang concurrent programming
Runtime package for golang concurrent programming
2022-06-22 03:23:00 【ManNiaoQinFen】
runtime The package defines some related to collaborative process management api
runtime.Gosched()
Give up CPU Time slice , Wait for the task to be rescheduled ( The time slice is relatively short )
package main
import (
"fmt"
)
func show(s string) {
for i := 0; i < 3; i++ {
fmt.Println(s)
}
}
func main() {
go show("JAVA")
go show("PHP")
// Main process
for i := 0; i < 2; i++ {
//runtime.Gosched() // The results are different before and after annotation
fmt.Println("golang:", i)
}
}
After comment ( It is also possible to complete the execution )
golang: 0
golang: 1
JAVA
No comment ( Probably all subroutines are completed )
PHP
PHP
PHP
golang: 0
JAVA
JAVA
JAVA
golang: 1
runtime.Goexit()
Exit the current collaboration
package main
import (
"fmt"
"runtime"
"time"
)
func show() {
for i := 0; i < 10; i++ {
if i > 5 {
runtime.Goexit() // Before and after use
}
fmt.Printf("i: %v\n", i)
}
}
func main() {
go show()
time.Sleep(time.Second)
}
Unused run results
i: 0
i: 1
i: 2
i: 3
i: 4
i: 5
i: 6
i: 7
i: 8
i: 9
After using
i: 0
i: 1
i: 2
i: 3
i: 4
i: 5
runtime.GOMAXPROCS
Use the largest cpu Count
package main
import (
"fmt"
"runtime"
"time"
)
func a() {
for i := 0; i < 10; i++ {
fmt.Println("a:", i)
}
}
func b() {
for i := 0; i < 10; i++ {
fmt.Println("b:", i)
}
}
func main() {
fmt.Printf("runtime.NumCPU(): %v\n", runtime.NumCPU())
runtime.GOMAXPROCS(2) // It is amended as follows 1 when , Do not alternate , Greater than 1 when , Alternate execution
go a()
go b()
time.Sleep(time.Second)
}
边栏推荐
- Force buckle 461 Hamming distance
- Basic structure and application of backlight module
- [nvme2.0b 5] sous - système nvm
- Database interview summary
- On the auto increment of int type primary key in MySQL
- FastDFS-6.0.6
- Figure database ongdb release v-1.0.2
- 调度功能:splunk-operator-controller-manager
- 【爬虫笔记2】鼠标事件与截图方法、常用攻击方法
- 【leetcode周赛总结】LeetCode第298场周赛总结(6.19)
猜你喜欢

策略模式

ASUS reinstallation system keyboard lamp failure = & gt; Refitting the ATK drive

TX2 attaching SD card, jtop

Check information on the Internet after the college entrance examination, and pay attention to prevent websites without SSL certificates

工厂模式

Policy mode

Flink CDC MongoDB Connector 的实现原理和使用实践

Are you a technology manager or a project manager?

六、MySQL之数据定义语言(一)
![[nvme2.0b 8] nvme queue arbitration mechanism](/img/35/c5098623c14749711b205ef97c34a7.png)
[nvme2.0b 8] nvme queue arbitration mechanism
随机推荐
Harmonyos Hongmeng uses ORM bee to access database instances
golang并发编程之runtime包
2022 low voltage electrician test question simulation test question bank and answers
泛微 E-cology V9 信息泄露漏洞
Use the serialize common command
Vs loading symbols causes program to start slowly
Are you a technology manager or a project manager?
The most popular massive Alibaba cloud disk resource sharing
Kubernetes介绍和使用
sequelize 常用命令使用
Modèle stratégique
tx2镜像源设置
CMD view the console output of hearts, diamonds, spades and clubs to solve the garbled code
opencv安装(x86/tx2 cuda/共享库)
uv_run流程
Figure base de données ongdb version V - 1.0.2
Project Management-软件开发之项目管理
uv_loop_init()流程
[microservices | Nacos] quickly realize the configuration center function of Nacos, and complete configuration update and version iteration
TX2挂载SD卡,jtop