Created
March 23, 2018 07:54
-
-
Save wjx0912/3d11fea0079fcac86b5464e702d7d249 to your computer and use it in GitHub Desktop.
golang timeout1 (总个子任务超时)
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" | |
"time" | |
) | |
func add(ch chan int) { | |
for i := 0; i < 10; i++ { | |
ch <- i | |
} | |
} | |
func main() { | |
ticker := time.NewTicker(2 * time.Second) | |
defer ticker.Stop() | |
ch := make(chan int, 10) | |
go add(ch) | |
for { | |
select { | |
case <- ch: | |
time.Sleep(1 * time.Second) | |
fmt.Println("sleep one seconds ...") | |
case <- ticker.C: | |
fmt.Println("timeout") | |
return | |
default: | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment