Skip to content

Instantly share code, notes, and snippets.

@tmtk75
Created February 18, 2015 23:40
Show Gist options
  • Save tmtk75/324dcb82d362c0e2000d to your computer and use it in GitHub Desktop.
Save tmtk75/324dcb82d362c0e2000d to your computer and use it in GitHub Desktop.
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