Repository navigation
Expand file tree
/
Copy pathPubSubLock.java
More file actions
120 lines (99 loc) · 2.97 KB
/
Copy pathPubSubLock.java
File metadata and controls
120 lines (99 loc) · 2.97 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
package com.example;
import java.util.Random;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class PubSubLock {
public static void main(String[] args) {
Buffer buffer = new Buffer();
new Producer(buffer, "Prod-0").start();
new Consumer(buffer, "Cons-0").start();
}
private static class Buffer {
private Lock lock = new ReentrantLock(true);
private Condition bufferFull = lock.newCondition();
private Condition bufferEmpty = lock.newCondition();
private int count, index;
private int[] buffer = new int[10];
public void put(int item) {
lock.lock();
try {
while(count == buffer.length) {
System.out.println("[" + Thread.currentThread().getName() + "] Buffer FULL !! Producer waiting ...");
bufferFull.await();
}
System.out.println("[" + Thread.currentThread().getName() + "] Producer Index: " + index);
buffer[index++] = item;
count++;
System.out.println("[" + Thread.currentThread().getName() + "] Buffer Items: " + count + ", " + buffer.length);
bufferEmpty.signal();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public int get() {
lock.lock();
try {
while(count == 0) {
System.out.println("[" + Thread.currentThread().getName() + "] Buffer Empty !! Consumer waiting ...");
bufferEmpty.await();
}
count--;
System.out.println("[" + Thread.currentThread().getName() + "] Consumer Index: " + index);
int get = buffer[--index];
bufferFull.signal();
return get;
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
return -1;
}
}
private static class Producer extends Thread {
private Buffer buffer;
private Random random = new Random();
public Producer(Buffer buffer, String threadName) {
super(threadName);
this.buffer = buffer;
}
@Override
public void run() {
while(true) {
int randomNumber = random.nextInt(Integer.MAX_VALUE);
System.out.println("[" + Thread.currentThread().getName() + "] Producer produced: " + randomNumber);
this.buffer.put(randomNumber);
/*try {
Thread.sleep(10);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
}
}
}
private static class Consumer extends Thread {
private Buffer buffer;
public Consumer(Buffer buffer, String threadName) {
super(threadName);
this.buffer = buffer;
}
@Override
public void run() {
while(true) {
int consumed = this.buffer.get();
System.out.println("[" + Thread.currentThread().getName() + "] Consumer consumed: " + consumed);
try {
System.out.println("[" + Thread.currentThread().getName() + "] Consumer working for 10 seconds");
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}