我需要从一组阻塞队列中读取数据。阻塞队列是由我使用的库创建的。我的代码必须从队列中读取。我不想为这些阻塞队列中的每一个创建一个读取器线程。相反,我想使用单个线程(或者可能最多使用 2/3 个线程)轮询它们以了解数据的可用性。由于一些阻塞队列可能长时间没有数据,而其中一些可能会收到突发数据。轮询具有较小超时值的队列是可行的,但这根本没有效率,因为即使其中一些队列长时间没有数据,它仍然需要继续循环遍历所有队列。基本上,我正在寻找一种关于阻塞队列的 select/epoll(用于套接字)机制。任何线索真的很感激。


不过,在 Go 中做到这一点真的很容易。下面的代码使用通道和 goroutines 模拟相同的内容:


package main


import "fmt"

import "time"

import "math/rand"


func sendMessage(sc chan string) {

    var i int


    for {

        i =  rand.Intn(10)

        for ; i >= 0 ; i-- {

            sc <- fmt.Sprintf("Order number %d",rand.Intn(100))

        }

        i = 1000 + rand.Intn(32000);

        time.Sleep(time.Duration(i) * time.Millisecond)

    }

}


func sendNum(c chan int) {

    var i int 

    for  {

        i = rand.Intn(16);

        for ; i >=  0; i-- {

            time.Sleep(20 * time.Millisecond)

            c <- rand.Intn(65534)

        }

        i = 1000 + rand.Intn(24000);

        time.Sleep(time.Duration(i) * time.Millisecond)

    }

}


func main() {

    msgchan := make(chan string, 32)

    numchan := make(chan int, 32)

    i := 0

    for ; i < 8 ; i++ {

        go sendNum(numchan)

        go sendMessage(msgchan)

    }

    for {

        select {

        case msg := <- msgchan:

            fmt.Printf("Worked on  %s\n", msg)

        case x := <- numchan:

            fmt.Printf("I got %d \n", x)

        }

    }

}