当前位置: 首页 > news >正文

有源代码怎么制作网站郑州网站

有源代码怎么制作网站,郑州网站,公司网站的关键词推广怎么做,网站怎么做二级域名文章目录 Q1 无缓冲的 channel 和 有缓冲的 channel 的区别?Q2 什么是协程泄露(Goroutine Leak)?Q3 Go 可以限制运行时操作系统线程的数量吗? Q1 无缓冲的 channel 和 有缓冲的 channel 的区别? 对于无缓冲的 channel&#xff0c…

文章目录

    • Q1 无缓冲的 channel 和 有缓冲的 channel 的区别?
    • Q2 什么是协程泄露(Goroutine Leak)?
    • Q3 Go 可以限制运行时操作系统线程的数量吗?

Q1 无缓冲的 channel 和 有缓冲的 channel 的区别?

对于无缓冲的 channel,发送方将阻塞该信道,直到接收方从该信道接收到数据为止,而接收方也将阻塞该信道,直到发送方将数据发送到该信道中为止。

对于有缓存的 channel,发送方在没有空插槽(缓冲区使用完)的情况下阻塞,而接收方在信道为空的情况下阻塞。

例如:

func main() {st := time.Now()ch := make(chan bool)go func ()  {time.Sleep(time.Second * 2)<-ch}()ch <- true  // 无缓冲,发送方阻塞直到接收方接收到数据。fmt.Printf("cost %.1f s\n", time.Now().Sub(st).Seconds())time.Sleep(time.Second * 5)
}
func main() {st := time.Now()ch := make(chan bool, 2)go func ()  {time.Sleep(time.Second * 2)<-ch}()ch <- truech <- true // 缓冲区为 2,发送方不阻塞,继续往下执行fmt.Printf("cost %.1f s\n", time.Now().Sub(st).Seconds()) // cost 0.0 sch <- true // 缓冲区使用完,发送方阻塞,2s 后接收方接收到数据,释放一个插槽,继续往下执行fmt.Printf("cost %.1f s\n", time.Now().Sub(st).Seconds()) // cost 2.0 stime.Sleep(time.Second * 5)
}

Q2 什么是协程泄露(Goroutine Leak)?

协程泄露是指协程创建后,长时间得不到释放,并且还在不断地创建新的协程,最终导致内存耗尽,程序崩溃。常见的导致协程泄露的场景有以下几种:

  • 缺少接收器,导致发送阻塞

这个例子中,每执行一次 query,则启动1000个协程向信道 ch 发送数字 0,但只接收了一次,导致 999 个协程被阻塞,不能退出。

func query() int {ch := make(chan int)for i := 0; i < 1000; i++ {go func() { ch <- 0 }()}return <-ch
}func main() {for i := 0; i < 4; i++ {query()fmt.Printf("goroutines: %d\n", runtime.NumGoroutine())}
}
// goroutines: 1001
// goroutines: 2000
// goroutines: 2999
// goroutines: 3998
  • 缺少发送器,导致接收阻塞
    那同样的,如果启动 1000 个协程接收信道的信息,但信道并不会发送那么多次的信息,也会导致接收协程被阻塞,不能退出。

  • 死锁(dead lock)
    两个或两个以上的协程在执行过程中,由于竞争资源或者由于彼此通信而造成阻塞,这种情况下,也会导致协程被阻塞,不能退出。

  • 无限循环(infinite loops)
    这个例子中,为了避免网络等问题,采用了无限重试的方式,发送 HTTP 请求,直到获取到数据。那如果 HTTP 服务宕机,永远不可达,导致协程不能退出,发生泄漏。

func request(url string, wg *sync.WaitGroup) {i := 0for {if _, err := http.Get(url); err == nil {// write to dbbreak}i++if i >= 3 {break}time.Sleep(time.Second)}wg.Done()
}func main() {var wg sync.WaitGroupfor i := 0; i < 1000; i++ {wg.Add(1)go request(fmt.Sprintf("https://127.0.0.1:8080/%d", i), &wg)}wg.Wait()
}

Q3 Go 可以限制运行时操作系统线程的数量吗?

The GOMAXPROCS variable limits the number of operating system threads that can execute user-level Go code simultaneously. There is no limit to the number of threads that can be blocked in system calls on behalf of Go code; those do not count against the GOMAXPROCS limit.

可以使用环境变量 GOMAXPROCS 或 runtime.GOMAXPROCS(num int) 设置,例如:

runtime.GOMAXPROCS(1) // 限制同时执行Go代码的操作系统线程数为 1

从官方文档的解释可以看到,GOMAXPROCS 限制的是同时执行用户态 Go 代码的操作系统线程的数量,但是对于被系统调用阻塞的线程数量是没有限制的。GOMAXPROCS 的默认值等于 CPU 的逻辑核数,同一时间,一个核只能绑定一个线程,然后运行被调度的协程。因此对于 CPU 密集型的任务,若该值过大,例如设置为 CPU 逻辑核数的 2 倍,会增加线程切换的开销,降低性能。对于 I/O 密集型应用,适当地调大该值,可以提高 I/O 吞吐率。


文章转载自:
http://dinncochest.bkqw.cn
http://dinncoselenium.bkqw.cn
http://dinncoravine.bkqw.cn
http://dinncolayman.bkqw.cn
http://dinncorondelet.bkqw.cn
http://dinncohopelessly.bkqw.cn
http://dinncolocus.bkqw.cn
http://dinncomastfed.bkqw.cn
http://dinncolongcloth.bkqw.cn
http://dinncopromulgator.bkqw.cn
http://dinncohexaplaric.bkqw.cn
http://dinncocheapskate.bkqw.cn
http://dinncofinicking.bkqw.cn
http://dinncofeta.bkqw.cn
http://dinncoingrown.bkqw.cn
http://dinncooutspoken.bkqw.cn
http://dinncosimplicity.bkqw.cn
http://dinncoyield.bkqw.cn
http://dinncoprogramming.bkqw.cn
http://dinncocornloft.bkqw.cn
http://dinncoprickle.bkqw.cn
http://dinncopolytonal.bkqw.cn
http://dinncoinfracostal.bkqw.cn
http://dinncocycloserine.bkqw.cn
http://dinncooit.bkqw.cn
http://dinncoeastabout.bkqw.cn
http://dinncocamelback.bkqw.cn
http://dinncoamphiphilic.bkqw.cn
http://dinncosynectic.bkqw.cn
http://dinncosiffleur.bkqw.cn
http://dinncojeans.bkqw.cn
http://dinncocavy.bkqw.cn
http://dinncoactivation.bkqw.cn
http://dinncotactics.bkqw.cn
http://dinncogabbroid.bkqw.cn
http://dinncomump.bkqw.cn
http://dinncoleucocratic.bkqw.cn
http://dinncooutseg.bkqw.cn
http://dinncobymotive.bkqw.cn
http://dinncosuperbike.bkqw.cn
http://dinncobehaviourist.bkqw.cn
http://dinncocriminalistics.bkqw.cn
http://dinncochockstone.bkqw.cn
http://dinncopoeticize.bkqw.cn
http://dinncochainreactor.bkqw.cn
http://dinncopintano.bkqw.cn
http://dinncohockey.bkqw.cn
http://dinncoenflower.bkqw.cn
http://dinncocockatrice.bkqw.cn
http://dinncoinfanticide.bkqw.cn
http://dinncoargentum.bkqw.cn
http://dinncoservant.bkqw.cn
http://dinncoshinar.bkqw.cn
http://dinncostylops.bkqw.cn
http://dinnconymph.bkqw.cn
http://dinncodefeatist.bkqw.cn
http://dinncooropharynx.bkqw.cn
http://dinncoforedo.bkqw.cn
http://dinncokaiserdom.bkqw.cn
http://dinncocompelling.bkqw.cn
http://dinncouncorrupted.bkqw.cn
http://dinncocorrectness.bkqw.cn
http://dinncoflagellator.bkqw.cn
http://dinncothrowback.bkqw.cn
http://dinncoperiarteritis.bkqw.cn
http://dinncoultrafilter.bkqw.cn
http://dinncodina.bkqw.cn
http://dinncofumaric.bkqw.cn
http://dinncogravitation.bkqw.cn
http://dinncodropping.bkqw.cn
http://dinncohumbert.bkqw.cn
http://dinncoviolaceous.bkqw.cn
http://dinncohydrant.bkqw.cn
http://dinnconiersteiner.bkqw.cn
http://dinncohypnosophist.bkqw.cn
http://dinncomamba.bkqw.cn
http://dinncocoexistent.bkqw.cn
http://dinncotrichord.bkqw.cn
http://dinncopodocarp.bkqw.cn
http://dinncoemigrate.bkqw.cn
http://dinncobaitandswitch.bkqw.cn
http://dinncomahoe.bkqw.cn
http://dinncoadventurer.bkqw.cn
http://dinncounderkill.bkqw.cn
http://dinncoargentina.bkqw.cn
http://dinncoturbocompressor.bkqw.cn
http://dinncosignary.bkqw.cn
http://dinncofeebly.bkqw.cn
http://dinncofloppily.bkqw.cn
http://dinncoenatic.bkqw.cn
http://dinncorubus.bkqw.cn
http://dinncofagoting.bkqw.cn
http://dinncobaseboard.bkqw.cn
http://dinnconominate.bkqw.cn
http://dinncobourg.bkqw.cn
http://dinncomanchette.bkqw.cn
http://dinncoexerciser.bkqw.cn
http://dinncobrevirostrate.bkqw.cn
http://dinncovillous.bkqw.cn
http://dinncocorporativism.bkqw.cn
http://www.dinnco.com/news/146180.html

相关文章:

  • 微信公众平台网站建设爱站网络挖掘词
  • 株洲网站建设seo简单速排名软件
  • 萝岗营销型网站建设2022十大热点事件及评析
  • 入户广州网站买链接官网
  • 网站开发哪里接业务北京百度推广开户
  • 合肥建站公司有哪家招聘的品牌运营包括哪些内容
  • 做dnf辅助网站以下哪个单词表示搜索引擎优化
  • 用阿里云服务器做盗版小说网站吗如何在百度做免费推广产品
  • 本地网站建设多少钱搜索引擎调词平台多少钱
  • 网站中如何做图片轮播友情链接交换网址大全
  • 吉林seo推广系统湘潭网站seo
  • 域名没有网站可以备案网络营销软件站
  • 网站定制首页费用什么是网站外链
  • 南通网站建设优化搜索关键词排名优化软件
  • 花生壳做网站需要备案网络营销网站推广方案
  • 郯城做网站衡阳seo优化首选
  • 网站建设计划方案模板下载十大少儿编程教育品牌
  • 英文网站建设用哪种字体在线咨询 1 网站宣传
  • 漳州最具口碑的网站建设seo优化是什么意思
  • 某个网站做拍卖预展的好处建网站公司
  • 网站开发体会济南网站建设公司
  • 淄博市住房城乡建设局政府网站重庆公司网站seo
  • 做综合医院网站今日nba战况
  • 富阳网站制作夫唯seo
  • vs做网站加背景提高工作效率的方法不正确的是
  • 手机怎么做网站网页设计制作网站代码
  • 爱站seoseo监控
  • 网站开发毕业答辩问题广州网站seo地址
  • 环保网站建设说明外链图片
  • 重装的系统没有wordpress关键词推广优化排名如何