github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/time/sleep.go (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package time
     6  
     7  // Sleepは現在のゴルーチンを少なくともdの期間だけ一時停止します。
     8  // 負数またはゼロの期間は、Sleepがすぐに戻る原因となります。
     9  func Sleep(d Duration)
    10  
    11  // Timer型は単一のイベントを表します。
    12  // Timerが時間切れになると、現在の時間がCに送信されます。
    13  // ただし、TimerがAfterFuncによって作成された場合は除きます。
    14  // TimerはNewTimerまたはAfterFuncで作成する必要があります。
    15  type Timer struct {
    16  	C         <-chan Time
    17  	initTimer bool
    18  }
    19  
    20  // Stop prevents the Timer from firing.
    21  // It returns true if the call stops the timer, false if the timer has already
    22  // expired or been stopped.
    23  //
    24  // For a func-based timer created with AfterFunc(d, f),
    25  // if t.Stop returns false, then the timer has already expired
    26  // and the function f has been started in its own goroutine;
    27  // Stop does not wait for f to complete before returning.
    28  // If the caller needs to know whether f is completed,
    29  // it must coordinate with f explicitly.
    30  //
    31  // For a chan-based timer created with NewTimer(d), as of Go 1.23,
    32  // any receive from t.C after Stop has returned is guaranteed to block
    33  // rather than receive a stale time value from before the Stop;
    34  // if the program has not received from t.C already and the timer is
    35  // running, Stop is guaranteed to return true.
    36  // Before Go 1.23, the only safe way to use Stop was insert an extra
    37  // <-t.C if Stop returned false to drain a potential stale value.
    38  // See the [NewTimer] documentation for more details.
    39  func (t *Timer) Stop() bool
    40  
    41  // NewTimerは、少なくともdの期間経過後に、
    42  // そのチャネルに現在の時刻を送信する新しいTimerを作成します。
    43  //
    44  // Before Go 1.23, the garbage collector did not recover
    45  // timers that had not yet expired or been stopped, so code often
    46  // immediately deferred t.Stop after calling NewTimer, to make
    47  // the timer recoverable when it was no longer needed.
    48  // As of Go 1.23, the garbage collector can recover unreferenced
    49  // timers, even if they haven't expired or been stopped.
    50  // The Stop method is no longer necessary to help the garbage collector.
    51  // (Code may of course still want to call Stop to stop the timer for other reasons.)
    52  //
    53  // Before Go 1.23, the channel associated with a Timer was
    54  // asynchronous (buffered, capacity 1), which meant that
    55  // stale time values could be received even after [Timer.Stop]
    56  // or [Timer.Reset] returned.
    57  // As of Go 1.23, the channel is synchronous (unbuffered, capacity 0),
    58  // eliminating the possibility of those stale values.
    59  //
    60  // The GODEBUG setting asynctimerchan=1 restores both pre-Go 1.23
    61  // behaviors: when set, unexpired timers won't be garbage collected, and
    62  // channels will have buffered capacity. This setting may be removed
    63  // in Go 1.27 or later.
    64  func NewTimer(d Duration) *Timer
    65  
    66  // Resetはタイマーを期間d後に期限切れにする。
    67  // タイマーがアクティブであった場合はtrue、期限切れまたは停止された場合はfalseを返す。
    68  //
    69  // For a func-based timer created with AfterFunc(d, f), Reset either reschedules
    70  // when f will run, in which case Reset returns true, or schedules f
    71  // to run again, in which case it returns false.
    72  // When Reset returns false, Reset neither waits for the prior f to
    73  // complete before returning nor does it guarantee that the subsequent
    74  // goroutine running f does not run concurrently with the prior
    75  // one. If the caller needs to know whether the prior execution of
    76  // f is completed, it must coordinate with f explicitly.
    77  //
    78  // For a chan-based timer created with NewTimer, as of Go 1.23,
    79  // any receive from t.C after Reset has returned is guaranteed not
    80  // to receive a time value corresponding to the previous timer settings;
    81  // if the program has not received from t.C already and the timer is
    82  // running, Reset is guaranteed to return true.
    83  // Before Go 1.23, the only safe way to use Reset was to Stop and
    84  // explicitly drain the timer first.
    85  // See the [NewTimer] documentation for more details.
    86  func (t *Timer) Reset(d Duration) bool
    87  
    88  // After waits for the duration to elapse and then sends the current time
    89  // on the returned channel.
    90  // It is equivalent to NewTimer(d).C.
    91  //
    92  // Before Go 1.23, this documentation warned that the underlying
    93  // Timer would not be recovered by the garbage collector until the
    94  // timer fired, and that if efficiency was a concern, code should use
    95  // NewTimer instead and call Timer.Stop if the timer is no longer needed.
    96  // As of Go 1.23, the garbage collector can recover unreferenced,
    97  // unstopped timers. There is no reason to prefer NewTimer when After will do.
    98  func After(d Duration) <-chan Time
    99  
   100  // AfterFuncは、指定した時間が経過した後、fを自身のゴルーチンで呼び出します。
   101  // Stopメソッドを使用して呼び出しをキャンセルするために使用できるTimerを返します。
   102  // 返されたTimerのCフィールドは使用されず、nilになります。
   103  func AfterFunc(d Duration, f func()) *Timer