-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathsync_queue.go
More file actions
131 lines (117 loc) · 2.65 KB
/
sync_queue.go
File metadata and controls
131 lines (117 loc) · 2.65 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//main package has examples shown
// in Hands-On Data Structures and algorithms with Go book
package main
// importing fmt package
import (
"fmt"
"math/rand"
"time"
)
// constants
const (
messagePassStart = iota
messageTicketStart
messagePassEnd
messageTicketEnd
)
//Queue class
type Queue struct {
waitPass int
waitTicket int
playPass bool
playTicket bool
queuePass chan int
queueTicket chan int
message chan int
}
// New method initialises queue
func (queue *Queue) New() {
queue.message = make(chan int)
queue.queuePass = make(chan int)
queue.queueTicket = make(chan int)
go func() {
var message int
for {
select {
case message = <-queue.message:
switch message {
case messagePassStart:
queue.waitPass++
case messagePassEnd:
queue.playPass = false
case messageTicketStart:
queue.waitTicket++
case messageTicketEnd:
queue.playTicket = false
}
if queue.waitPass > 0 && queue.waitTicket > 0 && !queue.playPass && !queue.playTicket {
queue.playPass = true
queue.playTicket = true
queue.waitTicket--
queue.waitPass--
queue.queuePass <- 1
queue.queueTicket <- 1
}
}
}
}()
}
// StartTicketIssue starts the ticket issue
func (queue *Queue) StartTicketIssue() {
queue.message <- messageTicketStart
<-queue.queueTicket
}
// EndTicketIssue ends the ticket issue
func (queue *Queue) EndTicketIssue() {
queue.message <- messageTicketEnd
}
//StartPass ends the Pass queue
func (queue *Queue) StartPass() {
queue.message <- messagePassStart
<-queue.queuePass
}
//EndPass ends the Pass queue
func (queue *Queue) EndPass() {
queue.message <- messagePassEnd
}
//ticketIssue starts and ends the ticket issue
func ticketIssue(queue *Queue) {
for {
// Sleep up to 10 seconds.
time.Sleep(time.Duration(rand.Intn(10000)) * time.Millisecond)
queue.StartTicketIssue()
fmt.Println("Ticket Issue starts")
// Sleep up to 2 seconds.
time.Sleep(time.Duration(rand.Intn(2000)) * time.Millisecond)
fmt.Println("Ticket Issue ends")
queue.EndTicketIssue()
}
}
//passenger method starts and ends the pass queue
func passenger(queue *Queue) {
for {
// Sleep up to 10 seconds.
time.Sleep(time.Duration(rand.Intn(10000)) * time.Millisecond)
queue.StartPass()
fmt.Println(" Passenger starts")
// Sleep up to 2 seconds.
time.Sleep(time.Duration(rand.Intn(2000)) * time.Millisecond)
fmt.Println(" Passenger ends")
queue.EndPass()
}
}
// main method
func main() {
var queue *Queue = &Queue{}
queue.New()
fmt.Println(queue)
var i int
for i = 0; i < 10; i++ {
go passenger(queue)
}
var j int
for j = 0; j < 5; j++ {
go ticketIssue(queue)
}
select {}
}