File tree Expand file tree Collapse file tree 2 files changed +8
-4
lines changed Expand file tree Collapse file tree 2 files changed +8
-4
lines changed Original file line number Diff line number Diff line change @@ -132,3 +132,6 @@ lv.setAdapter(adapter);
132
132
2 . 定义一个optFunc实现Option接口
133
133
3 . 构造结构体时,接收可变类型的Option
134
134
4 . 遍历options,调用option中的设置参数方法
135
+
136
+ ## 12. 工人列队模式(worker)
137
+ > 自定义工人数量,高性能处理任务
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ import "fmt"
5
5
// 工人模式
6
6
// 自定义工人数量,高效处理任务
7
7
8
- var JobQueue chan Job
8
+ var JobQueue = make ( chan Job , 1000 )
9
9
10
10
type Job struct {
11
11
Content string
@@ -22,15 +22,16 @@ type Worker struct {
22
22
23
23
func NewWorker (workerPool chan chan Job ) Worker {
24
24
return Worker {
25
- // WorkerPool: workerPool,
26
- JobChan : make (chan Job ),
27
- QuitChan : make (chan bool ),
25
+ WorkerPool : workerPool ,
26
+ JobChan : make (chan Job ),
27
+ QuitChan : make (chan bool ),
28
28
}
29
29
}
30
30
31
31
func (w Worker ) Start () {
32
32
go func () {
33
33
for {
34
+ // 将当前 worker 注册到 worker 队列中。
34
35
w .WorkerPool <- w .JobChan
35
36
select {
36
37
case job := <- w .JobChan :
You can’t perform that action at this time.
0 commit comments