Clarify in Timers that we give timer2 a real chance to fire
Closes #304
This commit is contained in:
@@ -21,21 +21,25 @@ func main() {
|
||||
|
||||
// The `<-timer1.C` blocks on the timer's channel `C`
|
||||
// until it sends a value indicating that the timer
|
||||
// expired.
|
||||
// fired.
|
||||
<-timer1.C
|
||||
fmt.Println("Timer 1 expired")
|
||||
fmt.Println("Timer 1 fired")
|
||||
|
||||
// If you just wanted to wait, you could have used
|
||||
// `time.Sleep`. One reason a timer may be useful is
|
||||
// that you can cancel the timer before it expires.
|
||||
// that you can cancel the timer before it fires.
|
||||
// Here's an example of that.
|
||||
timer2 := time.NewTimer(time.Second)
|
||||
go func() {
|
||||
<-timer2.C
|
||||
fmt.Println("Timer 2 expired")
|
||||
fmt.Println("Timer 2 fired")
|
||||
}()
|
||||
stop2 := timer2.Stop()
|
||||
if stop2 {
|
||||
fmt.Println("Timer 2 stopped")
|
||||
}
|
||||
|
||||
// Give the `timer2` enough time to fire, if it ever
|
||||
// was going to, to show it is in fact stopped.
|
||||
time.Sleep(2 * time.Second)
|
||||
}
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
fb413c9b1152a30107c53bf0a739a22c0056976b
|
||||
_cLT2ewHYO8
|
||||
36cae12a3ca529e473d7839e9573c3e0a202c2de
|
||||
gF7VLRz3URM
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// The first timer will expire ~2s after we start the
|
||||
// The first timer will fire ~2s after we start the
|
||||
// program, but the second should be stopped before it has
|
||||
// a chance to expire.
|
||||
// a chance to fire.
|
||||
$ go run timers.go
|
||||
Timer 1 expired
|
||||
Timer 1 fired
|
||||
Timer 2 stopped
|
||||
|
||||
Reference in New Issue
Block a user