Created
May 19, 2020 06:28
-
-
Save dtest11/bfed7ed39707b2e4b4f331b92589b7a6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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