Skip to content

Instantly share code, notes, and snippets.

@dtest11
Created May 19, 2020 06:28
Show Gist options
  • Save dtest11/bfed7ed39707b2e4b4f331b92589b7a6 to your computer and use it in GitHub Desktop.
Save dtest11/bfed7ed39707b2e4b4f331b92589b7a6 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math/rand"
"time"
)
func worker(pipeline chan<- int, counter int) {
t := rand.Intn(10)
d := time.Duration(t)
fmt.Println(t)
time.Sleep(time.Second * d)
pipeline <- counter
}
func main() {
timer := time.NewTimer(2 * time.Second)
pipeline := make(chan int, 100)
counter := 0
for {
timer.Reset(2 * time.Second)
counter++
go worker(pipeline, counter)
select {
case <-timer.C:
fmt.Println("time out")
case data := <-pipeline:
fmt.Println("receive ", data)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment