shitaibin.github.io
shitaibin.github.io copied to clipboard
Go面试题:并发 | 大彬 LIB
http://lessisbetter.site/2019/05/03/go-concurrent-problems1/
Once这个once的实现有没有什么问题? 1234567891011121314151617type Once struct { m sync.Mutex done uint32}func (o *Once) Do(f func()) { if o.done == 1 { return } o.m.Lock() defer o.m.Unl
那个单例那个是f只执行一次的,只是有可能在f第一次执行完毕之前,one就被置为1导致不可预料的结果,并不是会执行两次,他们后边又讨论了。。。博主可以去看下
@1071496910 多谢,已调整
channel 1 问题答案是对的,解析的不对,实际上存在的 goroutine 是 main 以及第二个传入 ch 的 goroutine,因为值传递的原因,它是堵塞的,ticker 也会有 goroutine,但是属于系统的,runtime.NumGoroutines并不包含系统 goroutine
@Bububuger channel 1 问题答案是对的,解析的不对,实际上存在的 goroutine 是 main 以及第二个传入 ch 的 goroutine,因为值传递的原因,它是堵塞的,ticker 也会有 goroutine,但是属于系统的,runtime.NumGoroutines并不包含系统 goroutine
感谢提醒,第2个goroutine实际传入的是nil的通道,造成阻塞;NumGoroutines确实不包含Ticker的通道