forked from go/golangs_learn
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1.0 KiB
43 lines
1.0 KiB
package main
|
|
|
|
import (
|
|
"fmt"
|
|
// "time"
|
|
)
|
|
|
|
func worker(id int, jobs <-chan int, results chan<- int) {
|
|
for j := range jobs {
|
|
fmt.Println("worker", "processing job", j)
|
|
// time.Sleep(time.Second)
|
|
results <- j
|
|
}
|
|
}
|
|
|
|
func ThreadPool() {
|
|
num := 100
|
|
loopNum := 3
|
|
jobs := make(chan int, num) //Job为100个可以传递int类型的channel
|
|
results := make(chan int, loopNum)
|
|
|
|
//开启三个线程,说明线程池中只有三个线程, 在实际情况下可以动态设置开启线程数量
|
|
for w := 1; w <= loopNum; w++ {
|
|
go worker(w, jobs, results)
|
|
}
|
|
|
|
// 添加9个任务
|
|
for j := 1; j <= num; j++ {
|
|
jobs <- j //向Jobs添加任务: 向Channel中写入数据, 传递的数据类型为int
|
|
}
|
|
//关闭Channel
|
|
close(jobs)
|
|
|
|
for a := 1; a <= num; a++ {
|
|
res := <-results //从Channel中读取数据, 输出的数据类型为 int
|
|
fmt.Println("ID = ", a, "Results = ", res) //注意 a 与 res 不对应是由于处理器调度的结果
|
|
}
|
|
}
|
|
|
|
// 开始执行
|
|
func main() {
|
|
ThreadPool()
|
|
}
|
|
|