-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer_exec.go
75 lines (65 loc) · 1.07 KB
/
timer_exec.go
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"fmt"
"sync"
"time"
)
var (
msgQue = make(chan int, 10)
quit = make(chan bool)
wait sync.WaitGroup
ticker = time.NewTicker(time.Second * 1)
)
func Proc() {
var iCnt int
for {
fmt.Println("start proc goroutine")
msgQue <- 1
time.Sleep(time.Second * 5)
iCnt += 1
if iCnt == 5 {
msgQue <- 0
break
}
}
wait.Done()
//通知心跳消息goroutine结束
fmt.Println("send heart quit channel")
quit <- true
}
func Cons() {
for {
fmt.Println("start consumer goroutine")
a := <-msgQue
fmt.Println("get: ", a)
if a == 0 {
break
}
}
wait.Done()
}
func HeartBt() {
for {
fmt.Println("heart beat for")
select {
case <-quit:
fmt.Println("proc and cons finished, heartbeat quit.")
return
case t := <-ticker.C:
fmt.Println("heart beat current time: ", t)
}
}
}
func main() {
defer ticker.Stop()
//创建三个goroutine,
//(1)模拟发送心跳消息
//(2)模拟接收行情
//(3)模拟处理行情
wait.Add(2)
go Proc()
go Cons()
go HeartBt()
wait.Wait()
fmt.Println("main finished.")
}