当前位置:网站首页>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 ”+”
边栏推荐
- Bitcoinwin (BCW) was invited to attend Hanoi traders fair 2022
- IBM大面积辞退40岁+的员工,掌握这十个搜索技巧让你的工作效率至上提高十倍
- Postman core function analysis - parameterization and test report
- 【C语言】字符串函数及模拟实现strlen&&strcpy&&strcat&&strcmp
- 使用easyexcel模板导出的两个坑(Map空数据列错乱和不支持嵌套对象)
- 95后阿里P7晒出工资单:狠补了这个,真香...
- The city chain technology Digital Innovation Strategy Summit was successfully held
- leetcode刷题:二叉树12(二叉树的所有路径)
- 【obs】libobs-winrt :CreateDispatcherQueueController
- 软件测试是干什么的?学习有啥要求?
猜你喜欢
司空见惯 - 英雄扫雷鼠
不愧是大佬,字节大牛耗时八个月又一力作
95后阿里P7晒出工资单:狠补了这个,真香...
Force buckle 1200 Minimum absolute difference
acm入门day1
leetcode刷题:二叉树11(平衡二叉树)
leetcode刷题:二叉树17(从中序与后序遍历序列构造二叉树)
Necessary skills for interview in large factories, 2022android will not die, I will not fall
Android interview classic, 2022 Android interview written examination summary
[untitled]
随机推荐
淺淺的談一下ThreadLocalInsecureRandom
What does software testing do? What are the requirements for learning?
selenium 元素信息
webuploader文件上传 拖拽上传 进度监听 类型控制 上传结果监听控件
Based on vs2017 and cmake GUI configuration, zxing and opencv are used in win10 x64 environment, and simple detection of data matrix code is realized
期货如何网上开户?安不安全?
常用运算符与运算符优先级
城链科技数字化创新战略峰会圆满召开
【obs】libobs-winrt :CreateDispatcherQueueController
Recommended collection, my Tencent Android interview experience sharing
[hard core dry goods] which company is better in data analysis? Choose pandas or SQL
Debezium series: PostgreSQL loads the correct last submission LSN from the offset
leetcode刷题:二叉树11(平衡二叉树)
618 "low key" curtain call, how can baiqiushangmei join hands with the brand to cross the "uncertain era"?
函数的概念及语法
Force buckle 1200 Minimum absolute difference
Reptile exercises (II)
UWB ultra wideband positioning technology, real-time centimeter level high-precision positioning application, ultra wideband transmission technology
深度学习 卷积神经网络(CNN)基础
c——顺序结构