Created
May 3, 2020 13:41
-
-
Save sotex/b039738bfccca3223c5f94b1adb58b60 to your computer and use it in GitHub Desktop.
Go 并发控制
This file contains hidden or 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 "log" | |
import "time" | |
import "sync" | |
var( | |
ch chan int | |
wg sync.WaitGroup | |
) | |
func init(){ | |
ch = make(chan int,4) | |
} | |
func main() { | |
for i:=0; i<10; i++ { | |
ch <- i | |
wg.Add(1) | |
go func(x int){ | |
defer func(){ | |
i := <- ch | |
log.Println("i = ",i) | |
wg.Done() | |
}() | |
log.Println("休眠前",x) | |
time.Sleep(time.Second) | |
}(i) | |
} | |
wg.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment