Multithreading 如何在不等待的情况下从通道获取值
在Go中,如果我尝试从通道接收,程序的执行将停止,直到某个值出现在通道中。但是,我想做的是让程序继续执行,如果通道中有值,就执行它 我想到的伪代码是这样的:Multithreading 如何在不等待的情况下从通道获取值,multithreading,asynchronous,synchronization,go,goroutine,Multithreading,Asynchronous,Synchronization,Go,Goroutine,在Go中,如果我尝试从通道接收,程序的执行将停止,直到某个值出现在通道中。但是,我想做的是让程序继续执行,如果通道中有值,就执行它 我想到的伪代码是这样的: mychan := make(chan int, 1) go someGoRoutine(mychan) // This might put some value in mychan at some point for { if something in "mychan" { // Remove the elem
mychan := make(chan int, 1)
go someGoRoutine(mychan) // This might put some value in mychan at some point
for {
if something in "mychan" {
// Remove the element from "mychan" and process it
} else {
// Other code
}
}
据我所知,我不能简单地使用v这就是它的用途。例如:
for {
select {
case v := <-c1:
// process v
case v, ok := <-c2:
// Second form, '!ok' -> c2 was closed
default:
// receiving was not done
}
}
用于{
挑选{
案例五:=这就是原因。例如:
for {
select {
case v := <-c1:
// process v
case v, ok := <-c2:
// Second form, '!ok' -> c2 was closed
default:
// receiving was not done
}
}
用于{
挑选{
案例五:=也许你需要更少的缓冲区大小,通常你需要有一些价值来做一些事情,这取决于你的业务,但你会得到更少的数据,并为每一个线程运行。我必须说,没有保证在较低的缓冲区大小下更快地获得数据,因为这取决于读卡器实现何时响应某些数据也许你需要less缓冲区大小,通常您需要有一些值来做一些事情,这取决于您的业务,但您将获得较少的数据并为每个线程运行线程。我必须说,不能保证在较低的缓冲区大小中获得更快的数据,因为响应某些数据的时间取决于读取器实现