concurrency - Go 超时 sleep 但不忙等待

标签 concurrency go timeout thread-sleep busy-waiting

在 Go 中,我可以使用 time.After 使休眠函数超时,但我不能对忙等待(或工作)的函数执行相同的操作。以下代码在一秒后返回 timed out,然后挂起。

package main

import (
        "fmt"
        "time"
)

func main() {
        sleepChan := make(chan int)
        go sleep(sleepChan)
        select {
        case sleepResult := <-sleepChan:
                fmt.Println(sleepResult)
        case <-time.After(time.Second):
                fmt.Println("timed out")
        }

        busyChan := make(chan int)
        go busyWait(busyChan)
        select {
        case busyResult := <-busyChan:
                fmt.Println(busyResult)
        case <-time.After(time.Second):
                fmt.Println("timed out")
        }
}

func sleep(c chan<- int) {
        time.Sleep(10 * time.Second)
        c <- 0
}

func busyWait(c chan<- int) {
        for {
        }
        c <- 0
}

为什么在第二种情况下不触发超时,我需要使用什么替代方法来中断工作中的 goroutines?

最佳答案

for {} 语句是一个独占单个处理器的无限循环。设置runtime.GOMAXPROCS到 2 或更多以允许计时器运行。

例如,

package main

import (
    "fmt"
    "runtime"
    "time"
)

func main() {
    fmt.Println(runtime.GOMAXPROCS(0))
    runtime.GOMAXPROCS(runtime.NumCPU())
    fmt.Println(runtime.GOMAXPROCS(0))
    sleepChan := make(chan int)
    go sleep(sleepChan)
    select {
    case sleepResult := <-sleepChan:
        fmt.Println(sleepResult)
    case <-time.After(time.Second):
        fmt.Println("timed out")
    }

    busyChan := make(chan int)
    go busyWait(busyChan)
    select {
    case busyResult := <-busyChan:
        fmt.Println(busyResult)
    case <-time.After(time.Second):
        fmt.Println("timed out")
    }
}

func sleep(c chan<- int) {
    time.Sleep(10 * time.Second)
    c <- 0
}

func busyWait(c chan<- int) {
    for {
    }
    c <- 0
}

输出(4 CPU 处理器):

1
4
timed out
timed out

关于concurrency - Go 超时 sleep 但不忙等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22265245/

相关文章:

.NET 客户端并发性能

java - Android 的 HttpResponse 和超时

java - 指定java对象的超时时间

javascript - 导航栏定期出现和消失

Scala Future 用于理解 : sequential vs parallel

scala - Scala对象和线程安全

java - 客户端从服务器请求公共(public)

go - 如何在 Go 中重构模块名称?

go - 我可以发出仅使用 HTTP/2 的 HTTP 请求吗

go - 为什么结果与标志 "-race"不同?