当前位置:网站首页>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 ”+”
边栏推荐
- Thread pool parameters and reasonable settings
- [Collection - industry solutions] how to build a high-performance data acceleration and data editing platform
- Debezium series: modify the source code to support UNIX_ timestamp() as DEFAULT value
- 【obs】libobs-winrt :CreateDispatcherQueueController
- 《乔布斯传》英文原著重点词汇笔记(十二)【 chapter ten & eleven】
- Debezium series: PostgreSQL loads the correct last submission LSN from the offset
- Complete interview questions for interviewers and senior Android engineers in front-line Internet enterprises
- Recommended collection, my Tencent Android interview experience sharing
- CADD课程学习(7)-- 模拟靶点和小分子相互作用 (半柔性对接 AutoDock)
- redis集群模拟消息队列
猜你喜欢

秋招字节面试官问你还有什么问题?其实你已经踩雷了

How about testing outsourcing companies?

leetcode刷题:二叉树13(相同的树)

Bitcoinwin (BCW)受邀参加Hanoi Traders Fair 2022

Elk distributed log analysis system deployment (Huawei cloud)

Using repositoryprovider to simplify the value passing of parent-child components

After 95, Alibaba P7 published the payroll: it's really fragrant to make up this

40000 word Wenshuo operator new & operator delete

That's awesome. It's enough to read this article

SecureRandom那些事|真伪随机数
随机推荐
软件测试工程师是做什么的?待遇前景怎么样?
Zhongang Mining: analysis of the current market supply situation of the global fluorite industry in 2022
全网最全的低代码/无代码平台盘点:简道云、伙伴云、明道云、轻流、速融云、集简云、Treelab、钉钉·宜搭、腾讯云·微搭、智能云·爱速搭、百数云
通过POI追加数据到excel中小案例
Float. The specific meaning of the return value of floattorawintbits is to convert float into byte array
How to safely and quickly migrate from CentOS to openeuler
CADD课程学习(7)-- 模拟靶点和小分子相互作用 (半柔性对接 AutoDock)
leetcode刷题:二叉树10(完全二叉树的节点个数)
深度学习 卷积神经网络(CNN)基础
After 95, Alibaba P7 published the payroll: it's really fragrant to make up this
Analysis of openh264 decoded data flow
完爆面试官,一线互联网企业高级Android工程师面试题大全
Is it safe to open a mobile stock account? Is it reliable?
third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl
leetcode刷题:二叉树15(找树左下角的值)
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
JVMRandom不可设置种子|问题追溯|源码追溯
618 "low key" curtain call, how can baiqiushangmei join hands with the brand to cross the "uncertain era"?
selenium 元素信息
Android interview, Android audio and video development