-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
58 lines (52 loc) · 835 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"fmt"
"time"
)
type Task struct {
ToBePrinted string
TimeOut time.Duration
Quit chan bool
}
var myTasks = []*Task{
{
ToBePrinted: "coucou1",
TimeOut: time.Second * 2,
},
{
ToBePrinted: "coucou2",
TimeOut: time.Second * 1,
},
{
ToBePrinted: "coucou3",
TimeOut: time.Second * 3,
},
}
func (myTask *Task) workerTask() {
for {
select {
case <-myTask.Quit:
{
fmt.Printf("%s stop working\n", myTask.ToBePrinted)
return
}
default:
{
fmt.Println(myTask.ToBePrinted)
time.Sleep(myTask.TimeOut)
}
}
}
}
func main() {
for i := 0; i < len(myTasks); i++ {
myTasks[i].Quit = make(chan bool, 1)
go myTasks[i].workerTask()
}
for {
var i int
fmt.Scanf("%d", &i)
fmt.Println("wants to stop: ", i)
myTasks[i].Quit <- true
}
}