惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Security Latest
Security Latest
C
CXSECURITY Database RSS Feed - CXSecurity.com
AI
AI
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
P
Palo Alto Networks Blog
Spread Privacy
Spread Privacy
Project Zero
Project Zero
Google Online Security Blog
Google Online Security Blog
The Cloudflare Blog
L
LangChain Blog
T
Tenable Blog
GbyAI
GbyAI
C
Cybersecurity and Infrastructure Security Agency CISA
大猫的无限游戏
大猫的无限游戏
Last Week in AI
Last Week in AI
量子位
Cloudbric
Cloudbric
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
美团技术团队
S
Secure Thoughts
P
Privacy & Cybersecurity Law Blog
S
Securelist
S
Schneier on Security
F
Full Disclosure
Engineering at Meta
Engineering at Meta
A
Arctic Wolf
Microsoft Security Blog
Microsoft Security Blog
Apple Machine Learning Research
Apple Machine Learning Research
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
爱范儿
爱范儿
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
P
Privacy International News Feed
宝玉的分享
宝玉的分享
A
About on SuperTechFans
博客园 - 【当耐特】
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hugging Face - Blog
Hugging Face - Blog
Webroot Blog
Webroot Blog
Google DeepMind News
Google DeepMind News
H
Heimdal Security Blog
Y
Y Combinator Blog
月光博客
月光博客
H
Hacker News: Front Page
MongoDB | Blog
MongoDB | Blog
C
Check Point Blog
TaoSecurity Blog
TaoSecurity Blog
腾讯CDC
N
Netflix TechBlog - Medium

博客园 - qufo

Docker 实现的 redis 主从 Golang 之 Base62 编码 Golang 之 Qrcode 二维码 Golang 之 key-value LevelDB 无废话 Thrift 之 Hello World( PHP 版). [转] putty 使用密钥登陆 OpenSSH 一个视图引发的血案 破一个行业ERP的感想 Practical Ext JS Projects with Gears中关于Gears描述。 七七前一天,搞定 PHP5 + Oracle 8.1.7 去掉PowerDesigner 15 在 Visual Studio 2008里的不兼容。 回家 庆祝自己通过驾驶员考试 2008.08.08.一个有记住意义的时刻。 adverbux.com 明天路考 blank.security 距离2008年8月8日还有8天。 随时10个可用线程--自己涂鸦的“线程池”
Golang 线程池
qufo · 2018-10-31 · via 博客园 - qufo

经常会用到协程,但是不能一下开那么多协调,只需要 poolSize 个即可,多了不行。这些个协程在执行完后必须等其完成之后才能进行下一步动作。假定工作方法为 work 。

package main

import (
    "fmt"
    "runtime"
    "sync"
    "time"
)
// WAIT GROUP
var wg sync.WaitGroup
var IDS_ALL = []int {40,42,43,44,164,166,171,173,174,175,177,518,192,193}

func main () {
    fmt.Println("BEGIN")

    poolSize := runtime.NumCPU()
    runtime.GOMAXPROCS(poolSize)

    ch := make(chan int,poolSize)
    for _,catid := range IDS_ALL {
        ch <- 1
        wg.Add(1)
        go work(catid,ch)
    }
    wg.Wait()
    close(ch)

    fmt.Println("DONE")
}

// 工作
func work(catid int,ch chan int) {

    fmt.Println("WORKING ",catid)
    time.Sleep(2 * 1e9)

    wg.Done()
    <-ch
}

就是这样。有问题,但能用。