Created
January 22, 2020 05:19
-
-
Save hiyali/3819620bc30b470cec68b782e4aafa34 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" | |
"time" | |
"sync" | |
) | |
func work(serverChan chan int, wg *sync.WaitGroup, server int, thing string) { | |
defer wg.Done() | |
// processing | |
time.Sleep(time.Duration(server) * time.Second) | |
now := time.Now() | |
fmt.Printf("%v, use server: %d, do thing: %s \n", now, server, thing) | |
serverChan <- server | |
} | |
func main() { | |
serverCount := 4 | |
serverChan := make(chan int, thingCount) | |
for i := 1; i <= serverCount; i++ { | |
serverChan <- i | |
} | |
thingCount := 10 | |
for i := 1; i <= thingCount; i++ { | |
wg.Add(1) | |
go work(serverChan, &wg, <- serverChan, fmt.Sprintf("thing %d", i)) | |
} | |
wg.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment