Skip to content

Commit 9fd3585

Browse files
authored
Update ProdConReentrantLock.java
1 parent ea93eb5 commit 9fd3585

File tree

1 file changed

+64
-61
lines changed

1 file changed

+64
-61
lines changed

ProdConReentrantLock.java

Lines changed: 64 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.concurrent.locks.Condition;
55
import java.util.concurrent.locks.Lock;
66
import java.util.concurrent.locks.ReentrantLock;
7+
import java.util.concurrent.TimeUnit;
78

89
public class ProdConReentrantLock {
910

@@ -15,8 +16,8 @@ public static void main(String[] args) {
1516
// creating producer and consumer threads
1617
Runnable prod = new Producer("PRODUCER", sharedResource);
1718
Runnable con = new Consumer("CONSUMER", sharedResource);
18-
Thread prodThread = new Thread(prod);
19-
Thread conThread = new Thread(con);
19+
Thread prodThread = new Thread(prod);
20+
Thread conThread = new Thread(con);
2021

2122
// starting producer and consumer threads
2223
prodThread.start();
@@ -28,7 +29,7 @@ public static void main(String[] args) {
2829
class Buffer {
2930
// producer consumer problem data
3031
private static final int CAPACITY = 10;
31-
private final Queue<Integer> queue = new LinkedList<>();
32+
private final Queue < Integer > queue = new LinkedList < > ();
3233
private final Random theRandom = new Random();
3334

3435
// lock and condition variables
@@ -37,74 +38,76 @@ class Buffer {
3738
private final Condition bufferNotEmpty = theLock.newCondition();
3839

3940
public void put() throws InterruptedException {
40-
while(true) {
41-
theLock.lock();
42-
try {
43-
while (queue.size() == CAPACITY) {
44-
System.out.println(Thread.currentThread().getName()
45-
+ " : Buffer is full, waiting");
46-
bufferNotEmpty.await();
47-
}
48-
49-
int number = theRandom.nextInt(1000);
50-
51-
queue.add(number);
52-
System.out.printf("%s produced %d into queue %n", Thread
53-
.currentThread().getName(), number);
54-
55-
// signal consumer thread that, buffer has element now
56-
System.out.println(Thread.currentThread().getName()
57-
+ " : Signalling that buffer is full now");
58-
bufferNotFull.signalAll();
59-
try {
60-
Thread.sleep(100);
61-
} catch(InterruptedException e) {
62-
System.err.println(e);
41+
while (true) {
42+
if (theLock.tryLock(1000, TimeUnit.MILLISECONDS)) {
43+
try {
44+
while (queue.size() == CAPACITY) {
45+
System.out.println(Thread.currentThread().getName() +
46+
" : Buffer is full, waiting");
47+
bufferNotEmpty.await();
48+
}
49+
50+
int number = theRandom.nextInt(1000);
51+
52+
queue.add(number);
53+
System.out.printf("%s produced %d into queue %n", Thread
54+
.currentThread().getName(), number);
55+
56+
// signal consumer thread that, buffer has element now
57+
System.out.println(Thread.currentThread().getName() +
58+
" : Signalling that buffer is full now");
59+
bufferNotFull.signalAll();
60+
try {
61+
TimeUnit.MILLISECONDS.sleep(100);
62+
} catch (InterruptedException e) {
63+
System.err.println(e);
64+
}
65+
} finally {
66+
theLock.unlock();
6367
}
64-
} finally {
65-
theLock.unlock();
66-
}
67-
}
68+
}
69+
}
6870
}
6971

7072
public void get() throws InterruptedException {
71-
while(true) {
72-
theLock.lock();
73-
try {
74-
while (queue.size() == 0) {
75-
System.out.println(Thread.currentThread().getName()
76-
+ " : Buffer is empty, waiting");
77-
bufferNotFull.await();
78-
}
79-
80-
Integer value = queue.poll();
81-
if (value != null) {
82-
System.out.printf("%s consumed %d from queue %n", Thread
83-
.currentThread().getName(), value);
84-
85-
// signal producer thread that, buffer may be empty now
86-
System.out.println(Thread.currentThread().getName()
87-
+ " : Signalling that buffer may be empty now");
88-
bufferNotEmpty.signalAll();
89-
}
90-
try {
91-
Thread.sleep(300);
92-
} catch(InterruptedException e) {
93-
System.err.println(e);
73+
while (true) {
74+
if (theLock.tryLock(1000, TimeUnit.MILLISECONDS)) {
75+
try {
76+
while (queue.size() == 0) {
77+
System.out.println(Thread.currentThread().getName() +
78+
" : Buffer is empty, waiting");
79+
bufferNotFull.await();
80+
}
81+
82+
Integer value = queue.poll();
83+
if (value != null) {
84+
System.out.printf("%s consumed %d from queue %n", Thread
85+
.currentThread().getName(), value);
86+
87+
// signal producer thread that, buffer may be empty now
88+
System.out.println(Thread.currentThread().getName() +
89+
" : Signalling that buffer may be empty now");
90+
bufferNotEmpty.signalAll();
91+
}
92+
try {
93+
TimeUnit.MILLISECONDS.sleep(300);
94+
} catch (InterruptedException e) {
95+
System.err.println(e);
96+
}
97+
} finally {
98+
theLock.unlock();
9499
}
95-
} finally {
96-
theLock.unlock();
97-
}
98-
}
100+
}
101+
}
99102
}
100103
}
101104

102105
class Producer implements Runnable {
103-
String name;
106+
String name;
104107
Buffer sharedResource;
105108

106109
public Producer(String name, Buffer sharedResource) {
107-
this.name = name;
110+
this.name = name;
108111
this.sharedResource = sharedResource;
109112
}
110113

@@ -119,11 +122,11 @@ public void run() {
119122
}
120123

121124
class Consumer implements Runnable {
122-
String name;
125+
String name;
123126
Buffer sharedResource;
124127

125128
public Consumer(String name, Buffer sharedResource) {
126-
this.name = name;
129+
this.name = name;
127130
this.sharedResource = sharedResource;
128131
}
129132

0 commit comments

Comments
 (0)