源代码
package threadpool
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 * 2
}
}
func ThreadPool() {
jobs := make(chan int, 100) //Job为100个可以传递int类型的channel
results := make(chan int, 100)
//开启三个线程,说明线程池中只有三个线程, 在实际情况下可以动态设置开启线程数量
for w := 1; w <= 3; w++ {
go worker(w, jobs, results)
}
// 添加9个任务
for j := 1; j <= 9; j++ {
jobs <- j //向Jobs添加任务: 向Channel中写入数据, 传递的数据类型为int
}
//关闭Channel
close(jobs)
for a := 1; a <= 9; a++ {
res := <-results //从Channel中读取数据, 输出的数据类型为 int
fmt.Println("ID = ", a, "Results = ", res) //注意 a 与 res 不对应是由于处理器调度的结果
}
}
测试代码
package threadpool
import (
"testing"
)
func TestTP(t *testing.T) {
ThreadPool()
}