-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
58 lines (40 loc) · 1.08 KB
/
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"
"math/rand"
"sync"
"time"
"github.com/Allyedge/operation-queue/operation"
"github.com/Allyedge/operation-queue/queue"
)
const MAXIMUM_WORKERS = 100
const MAXIMUM_CREATE_TIME = 100
const MAXIMUM_FINISH_TIME = 1000
func main() {
operationQueue := queue.Init()
notify := make(chan struct{}, 1)
wg := &sync.WaitGroup{}
for i := 0; i < MAXIMUM_WORKERS; i++ {
wg.Add(1)
go worker(wg, notify, &operationQueue)
}
for i := 0; i > -1; i++ {
random := rand.Intn(MAXIMUM_CREATE_TIME)
time.Sleep(time.Duration(random) * time.Millisecond)
operationQueue.Push(operation.Operation{ID: i})
fmt.Printf("+++ CREATE OPERATION %d +++\n", i)
notify <- struct{}{}
}
}
func worker(wg *sync.WaitGroup, notify chan struct{}, operationQueue *queue.Queue) {
defer wg.Done()
for range notify {
task := operationQueue.Pop()
doOperation(task)
}
}
func doOperation(operation operation.Operation) {
random := rand.Intn(MAXIMUM_FINISH_TIME)
time.Sleep(time.Duration(random) * time.Millisecond)
fmt.Printf("--- FINISH OPERATION %d ---\n", operation.ID)
}