当前位置:网站首页>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
- Build your own website (16)
- Securerandom things | true and false random numbers
- What does software testing do? What are the requirements for learning?
- 安信证券在网上开户安全吗?
- 挖财钱堂教育靠谱安全吗?
- XaaS 陷阱:万物皆服务(可能)并不是IT真正需要的东西
- Debezium series: parsing the default value character set
- 使用 RepositoryProvider简化父子组件的传值
- leetcode刷题:二叉树18(最大二叉树)
猜你喜欢

【合集- 行业解决方案】如何搭建高性能的数据加速与数据编排平台

Debezium series: record the messages parsed by debezium and the solutions after the MariaDB database deletes multiple temporary tables

【硬核干货】数据分析哪家强?选Pandas还是选SQL

Jvmrandom cannot set seeds | problem tracing | source code tracing

Two pits exported using easyexcel template (map empty data columns are disordered and nested objects are not supported)
![[Collection - industry solutions] how to build a high-performance data acceleration and data editing platform](/img/64/08faef0fccec93337f0716ac57dd8e.jpg)
[Collection - industry solutions] how to build a high-performance data acceleration and data editing platform
Complete interview questions for interviewers and senior Android engineers in front-line Internet enterprises

Necessary skills for interview in large factories, 2022android will not die, I will not fall

leetcode刷题:二叉树14(左叶子之和)

Postman core function analysis - parameterization and test report
随机推荐
acm入门day1
C application interface development foundation - form control (5) - grouping control
线程池参数及合理设置
力扣 1200. 最小绝对差
Bitcoinwin (BCW) was invited to attend Hanoi traders fair 2022
How about testing outsourcing companies?
leetcode刷题:二叉树16(路径总和)
Database logic processing function
The difference between ID selector and class selector
[untitled]
Reptile exercises (II)
Where is the operation of new bonds? Is it safer and more reliable to open an account
成功入职百度月薪35K,2022Android开发面试解答
随机数生成的四种方法|Random|Math|ThreadLocalRandom|SecurityRandom
软件测试工程师是做什么的?待遇前景怎么样?
Successful entry into Baidu, 35K monthly salary, 2022 Android development interview answer
Is it safe for Guohai Securities to open an account online?
众昂矿业:2022年全球萤石行业市场供给现状分析
Four methods of random number generation | random | math | threadlocalrandom | securityrandom
【obs】libobs-winrt :CreateDispatcherQueueController