如何正确停止计时器? var timer*time.timer func A(){ timer.Stop()//取消旧计时器 go B()//新计时器 } func B(){ 计时器=时间。新计时器(100*时间。毫秒) 挑选{ case
使用一个额外的、独立的取消信号。由于您已经有一个select语句,另一个通道是一个明显的选择:如何正确停止计时器? var timer*time.timer func A(){ timer.Stop()//取消旧计时器 go B()//新计时器 } func B(){ 计时器=时间。新计时器(100*时间。毫秒) 挑选{ case,go,goroutine,Go,Goroutine,使用一个额外的、独立的取消信号。由于您已经有一个select语句,另一个通道是一个明显的选择: var timer *time.Timer func A() { timer.Stop() // cancel old timer go B() // new timer } func B() { timer = time.NewTimer(100 * time.Millisecond) select { case <- timer.C: //
var timer *time.Timer
func A() {
timer.Stop() // cancel old timer
go B() // new timer
}
func B() {
timer = time.NewTimer(100 * time.Millisecond)
select {
case <- timer.C:
// do something for timeout, like change state
}
}
导入“时间”
变量计时器*时间计时器
var canceled=make(chan结构{})
func A(){
//取消所有当前的Bs
挑选{
case cancelled添加到上述答案中,如果您想一次取消所有服务员,您可以使用自己的计时器机制封装该行为,该机制可以被取消,在之后的通道中发送true或false,告诉您是否从所有服务员的取消或超时中醒来
import "time"
var canceled = make(chan struct{})
func A() {
// cancel all current Bs
select {
case canceled <- struct{}{}:
default:
}
go B()
}
func B() {
select {
case <-time.After(100 * time.Millisecond):
// do something for timeout, like change state
case <-canceled:
// aborted
}
}
游乐场连接:
请描述您试图解决的高级问题。您能详细说明您的问题吗?@aerokite超时时,切换到下一个状态。当收到请求时,保持该状态并启动一个新的计时器。@lxyscls此代码的真正问题是什么?100ms计时器没有停止。那又怎样?只需使用取消通道并选择bothI'am的ct怀疑“Cancelled@lxyscls如果你关闭取消频道而不是按它,每个goroutine都会退出。@lxyscls,你是对的。不确定我在想什么。但是关闭频道也会中止所有未来的B。
import "time"
var canceled = make(chan struct{})
func A() {
// cancel all current Bs
select {
case canceled <- struct{}{}:
default:
}
go B()
}
func B() {
select {
case <-time.After(100 * time.Millisecond):
// do something for timeout, like change state
case <-canceled:
// aborted
}
}
package main
import (
"fmt"
"time"
)
type CancellableTimer struct {
cancel chan bool
}
func NewCancellableTimer() *CancellableTimer {
return &CancellableTimer{
cancel: make(chan bool),
}
}
// internal wait goroutine wrapping time.After
func (c *CancellableTimer) wait(d time.Duration, ch chan bool) {
select {
case <-time.After(d):
ch <- true
case <-c.cancel:
ch <- false
}
}
// After mimics time.After but returns bool to signify whether we timed out or cancelled
func (c *CancellableTimer) After(d time.Duration) chan bool {
ch := make(chan bool)
go c.wait(d, ch)
return ch
}
// Cancel makes all the waiters receive false
func (c *CancellableTimer) Cancel() {
close(c.cancel)
}
// a goroutine waiting for cancellation
func B(t *CancellableTimer) {
select {
// timedOut will signify a timeout or cancellation
case timedOut := <-t.After(time.Second):
if timedOut {
fmt.Println("Time out!")
} else {
fmt.Println("Cancelled!")
}
}
}
func main() {
t := NewCancellableTimer()
// Start 3 goroutines that wait for different timeouts on the same timer
go B(t)
go B(t)
go B(t)
// sleep a bit before cancelling
time.Sleep(100 * time.Millisecond)
// cancel the timer and all its waiters
t.Cancel()
// this is just to collect the output
time.Sleep(time.Second)
}
Cancelled!
Cancelled!
Cancelled!