forked from anantjainaccolite/Producer-Consumer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogram.go
86 lines (73 loc) · 1.92 KB
/
program.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
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
package main
import (
"fmt"
"time"
)
//Consumer
type Consumer struct {
items *chan int
timestamp *chan string
}
//creating a new Consumer
func NewConsumer(items *chan int, timestamp *chan string) *Consumer {
return &Consumer{items: items, timestamp: timestamp}
}
//consuming items from the channel
func (c *Consumer) consume() {
fmt.Println("consume: Started")
for {
// time.Sleep(45 * time.Millisecond)
if len(*c.items) <= 0 {
continue
}
fmt.Println("consuming...")
item := <-*c.items
timestamps := <-*c.timestamp
fmt.Println("Received:", item, " which was produced at: ", timestamps)
}
}
//Producer
type Producer struct {
items *chan int
done *chan bool
timestamp *chan string
}
//creating a new Producer
func NewProducer(items *chan int, done *chan bool, timestamp *chan string) *Producer {
return &Producer{items: items, done: done, timestamp: timestamp}
}
//producing and sending the item through the channel
func (p *Producer) produce(max int) {
i := 1
fmt.Println("produce: Started")
for {
if len(*p.items) >= max {
continue
}
fmt.Println("starting.....")
*p.items <- i
fmt.Println("starting 1.....")
*p.timestamp <- time.Now().String()
fmt.Println("starting 2.....")
fmt.Println("produce: Sending ", i)
i++
// time.Sleep(30 * time.Millisecond)
}
// *p.done <- true // signal when done
// fmt.Println("produce: Done")
}
func main() {
var items = make(chan int) // channel to send items
var done = make(chan bool) // channel to control when production is done
var timestamp = make(chan string) //channel to add timestamps
// Start a goroutine for Produce.produce
len := 5
go NewProducer(&items, &done, ×tamp).produce(len)
fmt.Println("in betwenn")
// Start a goroutine for Consumer.consume
go NewConsumer(&items, ×tamp).consume()
time.Sleep(100 * time.Second)
// Finish the program when the production is done
fmt.Println("last")
// <-done
}