Clarify in Timers that we give timer2 a real chance to fire
Closes #304
This commit is contained in:
parent
cb8bf44eca
commit
b32972d2e4
@ -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
|
||||
|
34
public/timers
generated
34
public/timers
generated
@ -45,7 +45,7 @@ at <a href="tickers">tickers</a>.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
<a href="http://play.golang.org/p/_cLT2ewHYO8"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<a href="http://play.golang.org/p/gF7VLRz3URM"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
|
||||
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
|
||||
</pre></div>
|
||||
|
||||
@ -99,13 +99,13 @@ time. This timer will wait 2 seconds.</p>
|
||||
<td class="docs">
|
||||
<p>The <code><-timer1.C</code> blocks on the timer’s channel <code>C</code>
|
||||
until it sends a value indicating that the timer
|
||||
expired.</p>
|
||||
fired.</p>
|
||||
|
||||
</td>
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre> <span class="o"><-</span><span class="nx">timer1</span><span class="p">.</span><span class="nx">C</span>
|
||||
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">"Timer 1 expired"</span><span class="p">)</span>
|
||||
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">"Timer 1 fired"</span><span class="p">)</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
@ -115,21 +115,35 @@ expired.</p>
|
||||
<td class="docs">
|
||||
<p>If you just wanted to wait, you could have used
|
||||
<code>time.Sleep</code>. 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.</p>
|
||||
|
||||
</td>
|
||||
<td class="code">
|
||||
<td class="code leading">
|
||||
|
||||
<div class="highlight"><pre> <span class="nx">timer2</span> <span class="o">:=</span> <span class="nx">time</span><span class="p">.</span><span class="nx">NewTimer</span><span class="p">(</span><span class="nx">time</span><span class="p">.</span><span class="nx">Second</span><span class="p">)</span>
|
||||
<span class="k">go</span> <span class="kd">func</span><span class="p">()</span> <span class="p">{</span>
|
||||
<span class="o"><-</span><span class="nx">timer2</span><span class="p">.</span><span class="nx">C</span>
|
||||
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">"Timer 2 expired"</span><span class="p">)</span>
|
||||
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">"Timer 2 fired"</span><span class="p">)</span>
|
||||
<span class="p">}()</span>
|
||||
<span class="nx">stop2</span> <span class="o">:=</span> <span class="nx">timer2</span><span class="p">.</span><span class="nx">Stop</span><span class="p">()</span>
|
||||
<span class="k">if</span> <span class="nx">stop2</span> <span class="p">{</span>
|
||||
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">"Timer 2 stopped"</span><span class="p">)</span>
|
||||
<span class="p">}</span>
|
||||
</pre></div>
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
<p>Give the <code>timer2</code> enough time to fire, if it ever
|
||||
was going to, to show it is in fact stopped.</p>
|
||||
|
||||
</td>
|
||||
<td class="code">
|
||||
|
||||
<div class="highlight"><pre> <span class="nx">time</span><span class="p">.</span><span class="nx">Sleep</span><span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="nx">time</span><span class="p">.</span><span class="nx">Second</span><span class="p">)</span>
|
||||
<span class="p">}</span>
|
||||
</pre></div>
|
||||
|
||||
@ -142,15 +156,15 @@ Here’s an example of that.</p>
|
||||
|
||||
<tr>
|
||||
<td class="docs">
|
||||
<p>The first timer will expire ~2s after we start the
|
||||
<p>The first timer will fire ~2s after we start the
|
||||
program, but the second should be stopped before it has
|
||||
a chance to expire.</p>
|
||||
a chance to fire.</p>
|
||||
|
||||
</td>
|
||||
<td class="code">
|
||||
|
||||
<div class="highlight"><pre><span class="gp">$</span> go run timers.go
|
||||
<span class="go">Timer 1 expired</span>
|
||||
<span class="go">Timer 1 fired</span>
|
||||
<span class="go">Timer 2 stopped</span>
|
||||
</pre></div>
|
||||
|
||||
@ -170,7 +184,7 @@ a chance to expire.</p>
|
||||
</div>
|
||||
<script>
|
||||
var codeLines = [];
|
||||
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"time\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' timer1 := time.NewTimer(2 * time.Second)\u000A');codeLines.push(' \x3C-timer1.C\u000A fmt.Println(\"Timer 1 expired\")\u000A');codeLines.push(' timer2 := time.NewTimer(time.Second)\u000A go func() {\u000A \x3C-timer2.C\u000A fmt.Println(\"Timer 2 expired\")\u000A }()\u000A stop2 := timer2.Stop()\u000A if stop2 {\u000A fmt.Println(\"Timer 2 stopped\")\u000A }\u000A}\u000A');codeLines.push('');
|
||||
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"time\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' timer1 := time.NewTimer(2 * time.Second)\u000A');codeLines.push(' \x3C-timer1.C\u000A fmt.Println(\"Timer 1 fired\")\u000A');codeLines.push(' timer2 := time.NewTimer(time.Second)\u000A go func() {\u000A \x3C-timer2.C\u000A fmt.Println(\"Timer 2 fired\")\u000A }()\u000A stop2 := timer2.Stop()\u000A if stop2 {\u000A fmt.Println(\"Timer 2 stopped\")\u000A }\u000A');codeLines.push(' time.Sleep(2 * time.Second)\u000A}\u000A');codeLines.push('');
|
||||
</script>
|
||||
<script src="site.js" async></script>
|
||||
</body>
|
||||
|
Loading…
x
Reference in New Issue
Block a user