当前位置:网站首页>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】libobs-winrt :CreateDispatcherQueueController
- Redis cluster simulated message queue
- 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
- Wildcard selector
- 线程池参数及合理设置
- Realizing deep learning framework from zero -- LSTM from theory to practice [practice]
- The difference between ID selector and class selector
- Go language | 02 for loop and the use of common functions
- 使用 RepositoryProvider简化父子组件的传值
- 通配符选择器
猜你喜欢
40000 word Wenshuo operator new & operator delete
Go language | 03 array, pointer, slice usage
acm入门day1
Bitcoinwin (BCW) was invited to attend Hanoi traders fair 2022
No matter how busy you are, you can't forget safety
城链科技数字化创新战略峰会圆满召开
redis集群模拟消息队列
[C language] string function and Simulation Implementation strlen & strcpy & strcat & StrCmp
third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl
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
随机推荐
The difference between ID selector and class selector
Two pits exported using easyexcel template (map empty data columns are disordered and nested objects are not supported)
IBM has laid off 40 + year-old employees in a large area. Mastering these ten search skills will improve your work efficiency ten times
Summer Challenge harmonyos - realize message notification function
线程池参数及合理设置
Gstreamer中的task
okcc呼叫中心有什么作用
How to safely and quickly migrate from CentOS to openeuler
《乔布斯传》英文原著重点词汇笔记(十二)【 chapter ten & eleven】
What is the core value of testing?
S7-200smart uses V90 Modbus communication control library to control the specific methods and steps of V90 servo
Is it safe for Guosen Securities to open an account online?
【无标题】
Build your own website (16)
Necessary skills for interview in large factories, 2022android will not die, I will not fall
Go language | 02 for loop and the use of common functions
What is the function of okcc call center
14. Users, groups, and permissions (14)
深度學習 卷積神經網絡(CNN)基礎
使用easyexcel模板导出的两个坑(Map空数据列错乱和不支持嵌套对象)