当前位置:网站首页>Go language learning tutorial (16)
Go language learning tutorial (16)
2022-07-05 19:53:00 【Little Chen Gong】
One 、 Deadlock
* In the main goroutine No cache in the middle channel Add content or in the main goroutine To the channel Add content and the number of added content has been greater than channel The number of caches will cause deadlock
fatal error : all goroutines are asleep -deadlock!
* Deadlock : Multiple processes in the program (Golang in goroutine) Congestion caused by competing resources ( wait for ) state , And this state has been maintained , At this time, the thread is called deadlock
* stay Golang Use no cache in channel Be sure to pay attention to . The following is the simplest deadlock program
* In the main coordination process ch<-1, No cache channel Whether adding or removing data will block goroutine, There is no other code in the current program , Lord goroutine Will be blocked all the time , At this time, the Lord goroutine It's a deadlock
func main() {
ch := make(chan int)
ch <- 1
}
* The following code will not cause deadlock
* As can be seen from the code example , Using no cache channel when , Special attention should be paid to the operation in the main collaboration process channel Code
func main() {
ch := make(chan int)
go func() {
ch <- 1
fmt.Println(" perform goroutine")
}()
time.Sleep(5e9)
fmt.Println(" End of program execution ")
}
Two 、 There is a cache channel
* Create a cache channel
func main() {
ch := make(chan int, 3) // Cache size 3, The number of messages inside is less than or equal to 3 It won't block goroutine
ch <- 1
ch <- 2
ch <- 3
ch <- 4 // Deadlock on this line , Number of cache sizes exceeded
}
* stay Golang In the cache channel The cache size of cannot be changed , But as long as it doesn't exceed the number and size of caches , There will be no blocking
func main() {
ch := make(chan int, 3) // Cache size 3, The number of messages inside is less than or equal to 3 It won't block goroutine
ch <- 1
fmt.Println(<-ch)
ch <- 2
fmt.Println(<-ch)
ch <- 3
ch <- 4
fmt.Println(len(ch))// Output 2, Express channel There are two messages in
fmt.Println(cap(ch))// Output 3, Indicates that the total cache size is 3
}
3、 ... and 、select brief introduction
* Golang in select and switch The structure is especially like , however select in case The only condition can be I/O
* select The grammar of (condition Is the condition )
select{
case condition:
case condition:
default:
}
* select Execution process :
* Every case Must be a IO operation
* Which one? case You can execute whatever you want
* Multiple case Can be executed , Execute a random
* all case When it can't be executed , perform default
* all case Can't execute , And there's no default, Will block
func main() {
runtime.GOMAXPROCS(1)
ch1 := make(chan int, 1)
ch2 := make(chan string, 1)
ch1 <- 1
ch2 <- "hello"
select {
case value := <-ch1:
fmt.Println(value)
case value := <-ch2:
fmt.Println(value)
}
}
* select DUOHE for Recycling in combination , Here is an example of receiving messages all the time
func main() {
ch := make(chan int)
for i := 1; i <= 5; i++ {
go func(arg int) {
ch <- arg
}(i)
}
// If you have been receiving messages , It should be a dead cycle for{}, The following code clearly knows the number of messages
for i := 1; i <= 5; i++ {
select {
case c := <-ch:
fmt.Println(" Take out the data ", c)
default:
// No, default A deadlock occurs
}
}
fmt.Println(" End of program execution ")
}
* break It can be done to select take effect , If for Nested in select,break Select the nearest structure
Four 、GC
* GC English full name garbage collector
* Go Language GC Relative C/C++ A very important improvement in language
* Some commonly used GC Algorithm
* Quoting calculation : When an object is referenced, the calculator adds one . The unreferenced counter is decremented by one
* PHP and Object-C Use
* Cross references cannot be recycled
* Counting increases consumption
* Mark And Sweep Marking and clearing algorithm : Stop the program , Recursively traversing objects , marked . After marking, clear all objects that are not referenced
* The program needs to be stopped due to the marking (Stop the world), When there are many objects , The marking and clearing process is time-consuming ( Maybe hundreds of milliseconds ), It's hard to accept
* Tricolor notation : yes Mark And Sweep Improved version . Logically, it is divided into white areas ( Not searched ), Grey area ( Searching ), Black area ( Searched ). The content in the gray area is that sub references are not searched , The black area indicates that the sub reference exists
* Generational collection : Generally, there are three generations , for example java Meso Cenozoic , Old age , Forever . When there is a threshold in the new generation, the object will be put into the old age , Similarly, when the content of the elderly generation reaches the threshold, it will be put into the permanent generation
5、 ... and 、Go In language GC
* Go Use... In language Stop The World The way
* Golang Each version is basically right GC To optimize , from Golang1.5 Start supporting concurrency (concurrent ) collect , from 1.8 The version has put STW The time is optimized 100 subtle , Usually it's just 10 Below microseconds . And in 1.10 When the version is optimized again, it is reduced GC Yes CPU Occupy
* Go In language GC It's automatic , It will trigger GC
* When you need to request memory , Find out GC It was last time GC Double will trigger
* Every time 2 Automatically run once every minute GC
* GC tuning
* Small object reuse , Local variables should be declared as little as possible , Multiple small objects can be put into the structure , convenient GC scanning
* To use less string Of ”+”
边栏推荐
- [OBS] qstring's UTF-8 Chinese conversion to blog printing UTF-8 char*
- JVMRandom不可设置种子|问题追溯|源码追溯
- Build your own website (16)
- Interviewer: what is the internal implementation of set data types in redis?
- 手机股票开户安全吗?靠不靠谱啊?
- 挖财钱堂教育靠谱安全吗?
- 浮动元素与父级、兄弟盒子的关系
- Analysis of openh264 decoded data flow
- 【无标题】
- XaaS 陷阱:万物皆服务(可能)并不是IT真正需要的东西
猜你喜欢
Reinforcement learning - learning notes 4 | actor critical
2023年深圳市绿色低碳产业扶持计划申报指南
Interviewer: what is the internal implementation of set data types in redis?
【硬核干货】数据分析哪家强?选Pandas还是选SQL
third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl
S7-200SMART利用V90 MODBUS通信控制库控制V90伺服的具体方法和步骤
建议收藏,我的腾讯Android面试经历分享
Bitcoinwin (BCW)受邀参加Hanoi Traders Fair 2022
全网最全的低代码/无代码平台盘点:简道云、伙伴云、明道云、轻流、速融云、集简云、Treelab、钉钉·宜搭、腾讯云·微搭、智能云·爱速搭、百数云
leetcode刷题:二叉树10(完全二叉树的节点个数)
随机推荐
id选择器和类选择器的区别
Debezium series: record the messages parsed by debezium and the solutions after the MariaDB database deletes multiple temporary tables
Inventory of the most complete low code / no code platforms in the whole network: Jiandao cloud, partner cloud, Mingdao cloud, Qingliu, xurong cloud, Jijian cloud, treelab, nailing · Yida, Tencent clo
Debezium series: parsing the default value character set
【合集- 行业解决方案】如何搭建高性能的数据加速与数据编排平台
leetcode刷题:二叉树18(最大二叉树)
股票开户哪里好?网上客户经理开户安全吗
Successful entry into Baidu, 35K monthly salary, 2022 Android development interview answer
selenium 元素信息
How about testing outsourcing companies?
《乔布斯传》英文原著重点词汇笔记(十二)【 chapter ten & eleven】
Float. The specific meaning of the return value of floattorawintbits is to convert float into byte array
leetcode刷题:二叉树16(路径总和)
Autumn byte interviewer asked you any questions? In fact, you have stepped on thunder
深度学习 卷积神经网络(CNN)基础
Common - Hero Minesweeper
Is it safe for Anxin securities to open an account online?
Debezium series: PostgreSQL loads the correct last submission LSN from the offset
四万字长文说operator new & operator delete
Do you know several assertion methods commonly used by JMeter?