Skip to content

Commit 10a0275

Browse files
committed
Putting lock.lock() before try{} in MyBlockingQueue
1 parent 742543b commit 10a0275

File tree

2 files changed

+2
-6
lines changed

2 files changed

+2
-6
lines changed

src/com/lld/myblockingqueue/MyBlockingQueue.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ public MyBlockingQueue(int capacity) {
3030
public void put(E item) throws InterruptedException {
3131
Objects.requireNonNull(item);
3232

33+
lock.lock();
3334
try {
34-
lock.lock();
35-
3635
while (size.get() == capacity) {
3736
canPut.await();
3837
}
@@ -48,9 +47,8 @@ public void put(E item) throws InterruptedException {
4847
}
4948

5049
public E get() throws InterruptedException {
50+
lock.lock();
5151
try {
52-
lock.lock();
53-
5452
while (size.get() == 0) {
5553
canTake.await();
5654
}

src/com/lld/myblockingqueue/Producer.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.lld.myblockingqueue;
22

3-
import java.util.Random;
4-
53
public class Producer implements Runnable {
64
private MyBlockingQueue<Integer> myBlockingQueue;
75

0 commit comments

Comments
 (0)