Skip to content

Commit

Permalink
Sleep 实现说明
Browse files Browse the repository at this point in the history
  • Loading branch information
polaris1119 committed Jun 14, 2016
1 parent 272c93f commit 36a58c5
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions chapter04/04.4.md
Expand Up @@ -113,9 +113,13 @@ time.Sleep(10 * time.Second)

`Reset` 会先调用 `stopTimer` 再调用 `startTimer`,类似于废弃之前的定时器,重新启动一个定时器。返回值和 `Stop` 一样。

### Ticker 相关函数或方法的使用
### Sleep 的内部实现

`Ticker``Timer` 类似,区别是:`Ticker` 中的`runtimeTimer` 字段的 `period` 字段会赋值为 `NewTicker(d Duration)` 中的 `d`,表示每间隔 `d` 时间,定时器就会触发一次。
查看 `runtime/time.go` 文件中的 `timeSleep` 可知,`Sleep` 的是通过 `Timer` 实现的,把当前 goroutine 作为 `arg` 参数(`getg()`)。

## Ticker 相关函数或方法的使用

`Ticker``Timer` 类似,区别是:`Ticker` 中的`runtimeTimer` 字段的 `period` 字段会赋值为 `NewTicker(d Duration)` 中的 `d`,表示每间隔 `d` 纳秒,定时器就会触发一次。

除非程序终止前定时器一直需要触发,否则,不需要时应该调用 `Ticker.Stop` 来释放相关资源。

Expand Down

0 comments on commit 36a58c5

Please sign in to comment.