@@ -45,7 +45,6 @@ func start(id string, f func(string) int, next chan<- struct{}) (chan<- struct{}
45
45
}
46
46
}
47
47
}
48
-
49
48
}()
50
49
return queue , quit , result
51
50
}
@@ -90,7 +89,25 @@ func max(args ...int) int {
90
89
return n
91
90
}
92
91
93
- func main () {
92
+ func idCheck3 (id string ) int {
93
+ time .Sleep (time .Millisecond * time .Duration (idCheckTmCost ))
94
+ print ("\t goroutine-" , id , "-idCheck: idCheck ok\n " )
95
+ return idCheckTmCost
96
+ }
97
+
98
+ func bodyCheck3 (id string ) int {
99
+ time .Sleep (time .Millisecond * time .Duration (bodyCheckTmCost ))
100
+ print ("\t goroutine-" , id , "-bodyCheck: bodyCheck ok\n " )
101
+ return bodyCheckTmCost
102
+ }
103
+
104
+ func xRayCheck3 (id string ) int {
105
+ time .Sleep (time .Millisecond * time .Duration (xRayCheckTmCost ))
106
+ print ("\t goroutine-" , id , "-xRayCheck: xRayCheck ok\n " )
107
+ return xRayCheckTmCost
108
+ }
109
+
110
+ func showConcurrency3 () {
94
111
passengers := 30
95
112
queue := make (chan struct {}, 30 )
96
113
newAirportSecurityCheckChannel ("channel1" , queue )
@@ -105,3 +122,71 @@ func main() {
105
122
close (queue ) // 为了打印各通道的处理时长
106
123
time .Sleep (1000 * time .Second )
107
124
}
125
+
126
+ func start3 (id string , f func (string ) int , next chan struct {}) (sig chan struct {}, quit chan struct {}, data chan int ) {
127
+ sig = make (chan struct {}, 10 )
128
+ quit = make (chan struct {})
129
+ data = make (chan int )
130
+
131
+ go func () {
132
+ total := 0
133
+ for {
134
+ select {
135
+ case <- quit :
136
+ data <- total
137
+ return
138
+ case v := <- sig :
139
+ total += f (id )
140
+ if next != nil {
141
+ next <- v
142
+ }
143
+ }
144
+ }
145
+ }()
146
+ return sig , quit , data
147
+ }
148
+
149
+ func newAirportSecurityCheckChannel2 (s string , sig chan struct {}) {
150
+ println ("newAirportSecurityCheckChannel2 ready" )
151
+ go func () {
152
+ sig3 , quit1 , data1 := start3 (s , xRayCheck3 , nil )
153
+ sig2 , quit2 , data2 := start3 (s , bodyCheck3 , sig3 )
154
+ sig1 , quit3 , data3 := start3 (s , idCheck3 , sig2 )
155
+ for {
156
+ v , ok := <- sig
157
+ if ! ok {
158
+ close (quit1 )
159
+ close (quit2 )
160
+ close (quit3 )
161
+ total := max (<- data1 , <- data2 , <- data3 )
162
+ println ("gouroutine: " , s , ", total: " , total )
163
+ println ("gouroutine: " , s , ", closed" )
164
+ return
165
+ }
166
+ sig1 <- v
167
+ }
168
+ }()
169
+ }
170
+
171
+ func showConcurrency33 () {
172
+ passagers := 30
173
+ sig := make (chan struct {}, 30 )
174
+ newAirportSecurityCheckChannel2 ("channel1" , sig )
175
+ newAirportSecurityCheckChannel2 ("channel2" , sig )
176
+ newAirportSecurityCheckChannel2 ("channel3" , sig )
177
+
178
+ time .Sleep (5 * time .Second )
179
+ for i := 0 ; i < passagers ; i ++ {
180
+ sig <- struct {}{}
181
+ }
182
+ time .Sleep (5 * time .Second )
183
+ println ("main done1" )
184
+ close (sig )
185
+ time .Sleep (1000 * time .Second )
186
+ println ("main done" )
187
+ }
188
+
189
+ func main () {
190
+ // showConcurrency3()
191
+ showConcurrency33 ()
192
+ }
0 commit comments