You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.2 KiB
69 lines
1.2 KiB
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
"flag"
|
|
"sync"
|
|
)
|
|
|
|
func Worker(name string, done chan<- int, stop <-chan int) {
|
|
for i := 0; i < 10; i++ {
|
|
select {
|
|
case <-stop:
|
|
fmt.Println("Worker stopped:", name)
|
|
return
|
|
default:
|
|
time.Sleep(1 * time.Second)
|
|
fmt.Println("TESTER #", name)
|
|
}
|
|
}
|
|
|
|
done<- 1
|
|
}
|
|
|
|
func Supervisor(id int, count int) {
|
|
stop := make(chan int)
|
|
var wg sync.WaitGroup
|
|
|
|
for i := 0; i < count; i++ {
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
|
|
for {
|
|
workerDone := make(chan int)
|
|
go Worker(fmt.Sprintf("%d:%d", id, i), workerDone, stop)
|
|
|
|
|
|
select {
|
|
case <-workerDone:
|
|
fmt.Println("Supervisor: Worker Done")
|
|
time.Sleep(500 * time.Millisecond)
|
|
case <-stop:
|
|
fmt.Println("Supervisor: Stop")
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
}
|
|
|
|
go func() {
|
|
// test shutdown
|
|
time.Sleep(10000 * time.Millisecond)
|
|
close(stop)
|
|
}()
|
|
|
|
wg.Wait()
|
|
}
|
|
|
|
|
|
func main() {
|
|
count := flag.Int("count", 10, "Seconds to wait")
|
|
id := flag.Int("id", 1, "Tester ID to use.")
|
|
flag.Parse()
|
|
|
|
fmt.Println(">>> TESTER STARTS, id=", *id, "count=", *count)
|
|
|
|
Supervisor(*id, *count)
|
|
}
|
|
|