Created
February 18, 2015 23:40
-
-
Save tmtk75/324dcb82d362c0e2000d to your computer and use it in GitHub Desktop.
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 ( | |
"fmt" | |
"sync" | |
"time" | |
) | |
type Task struct { | |
id int | |
} | |
func main() { | |
tasks := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} | |
jobs := make(chan Task, 3) | |
var wg sync.WaitGroup | |
wg.Add(len(tasks)) | |
for _, e := range []int{1, 2, 3} { | |
worker := func(id int) { | |
for t := range jobs { | |
defer wg.Done() | |
d := time.Duration(500) * time.Millisecond | |
fmt.Printf("worker[%v] task[%v] %v\n", id, t.id, d) | |
time.Sleep(d) | |
} | |
} | |
go worker(e) | |
} | |
for _, e := range tasks { | |
jobs <- Task{e} | |
} | |
close(jobs) | |
wg.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment