Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.22.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.22.1</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
28 changes: 21 additions & 7 deletions src/main/java/core/basesyntax/BlockingQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,38 @@
import java.util.Queue;

public class BlockingQueue<T> {
private Queue<T> queue = new LinkedList<>();
private int capacity;
private final Queue<T> queue = new LinkedList<>();
private final int capacity;

public BlockingQueue(int capacity) {
this.capacity = capacity;
}

public synchronized void put(T element) throws InterruptedException {
// write your code here

while (queue.size() == capacity) {
wait();
}

queue.add(element);

notifyAll();
}

public synchronized T take() throws InterruptedException {
// write your code here
return null;

while (queue.isEmpty()) {
wait();
}

T element = queue.poll();

notifyAll();
return element;
}

public synchronized boolean isEmpty() {
// write your code here
return true;

return queue.isEmpty();
}
}
15 changes: 11 additions & 4 deletions src/main/java/core/basesyntax/thread/Consumer.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
package core.basesyntax.thread;

import core.basesyntax.BlockingQueue;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Consumer implements Runnable {
private BlockingQueue<Integer> blockingQueue;

private static final Logger logger = LogManager.getLogger(Consumer.class);
private final BlockingQueue<Integer> blockingQueue;

public Consumer(BlockingQueue<Integer> blockingQueue) {
this.blockingQueue = blockingQueue;
}

@Override
public void run() {
while (!blockingQueue.isEmpty()) {
while (!Thread.currentThread().isInterrupted()) {
try {
System.out.println("Took value " + blockingQueue.take());
Integer value = blockingQueue.take();
System.out.println("Took value " + value);
} catch (InterruptedException e) {
throw new RuntimeException("Consumer was interrupted!", e);
Thread.currentThread().interrupt();
logger.warn("Thread was interrupted while taking from queue", e);
break;
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/core/basesyntax/thread/Producer.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package core.basesyntax.thread;

import core.basesyntax.BlockingQueue;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Producer implements Runnable {
private BlockingQueue<Integer> blockingQueue;

private static final Logger logger = LogManager.getLogger(Consumer.class);
private final BlockingQueue<Integer> blockingQueue;

public Producer(BlockingQueue<Integer> blockingQueue) {
this.blockingQueue = blockingQueue;
Expand All @@ -15,7 +19,9 @@ public void run() {
try {
blockingQueue.put(i);
} catch (InterruptedException e) {
throw new RuntimeException("Producer was interrupted!", e);
Thread.currentThread().interrupt();
logger.warn("Producer interrupted while putting value {}", i, e);
break;
}
}
}
Expand Down
Loading