-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoroutine.go
More file actions
36 lines (30 loc) · 720 Bytes
/
goroutine.go
File metadata and controls
36 lines (30 loc) · 720 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package main
import (
"fmt"
"time"
)
func main() {
const totalExecuteNum = 6
const maxConcurrencyNum = 3
sig := make(chan string, maxConcurrencyNum)
res := make(chan string, totalExecuteNum)
defer close(sig)
defer close(res)
fmt.Printf("start concurrency execute %s \n", time.Now())
for i := 0; i < totalExecuteNum; i++ {
go wait6sec(sig, res, fmt.Sprintf("no%d", i))
}
for {
if len(res) >= totalExecuteNum {
break
}
}
fmt.Printf("end concurrency execute %s \n", time.Now())
}
func wait6sec(sig chan string, res chan string, name string) {
sig <- fmt.Sprintf("sig %s", name)
time.Sleep(6 * time.Second)
fmt.Printf("%s:end wait 6sec \n", name)
res <- fmt.Sprintf("sig %s", name)
<-sig
}