-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlearning.go
56 lines (49 loc) · 1.19 KB
/
learning.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
package main
import (
"fmt"
"time"
)
type ticker struct {
period time.Duration
ticker time.Ticker
}
type server struct {
doneChan chan bool
tickerA ticker
tickerB ticker
}
func createTicker(period time.Duration) *ticker {
return &ticker{period, *time.NewTicker(period)}
}
func (t *ticker) resetTicker() {
t.ticker.Stop()
t.ticker = *time.NewTicker(t.period)
}
func (s *server) listener() {
start := time.Now()
tickACount := 0
fmt.Println("Elapsed: 0")
for {
select {
case <-s.tickerA.ticker.C:
elapsed := time.Since(start)
fmt.Println("Elapsed: ", elapsed, " Ticker A")
tickACount++
if tickACount == 4 {
s.doneChan <- true
}
case <-s.tickerB.ticker.C:
s.tickerA.resetTicker()
elapsed := time.Since(start)
fmt.Println("Elapsed: ", elapsed, " Ticker B - Going to reset ticker A")
}
}
}
func main() {
doneChan := make(chan bool)
tickerA := createTicker(2 * time.Second)
tickerB := createTicker(5 * time.Second)
s := &server{doneChan, *tickerA, *tickerB}
go s.listener()
<-doneChan
}