From ec3544acf261579689d6c252fa9e6767761288cf Mon Sep 17 00:00:00 2001 From: Alexandr Baranov Date: Sat, 14 Mar 2026 13:18:22 +0300 Subject: [PATCH 1/3] add queue task --- .../tasks/queue/BoundedBlockingQueue.java | 42 +++++++++++++++---- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/src/main/java/hse/java/lectures/lecture6/tasks/queue/BoundedBlockingQueue.java b/src/main/java/hse/java/lectures/lecture6/tasks/queue/BoundedBlockingQueue.java index 5c686cff..a3af0b0d 100644 --- a/src/main/java/hse/java/lectures/lecture6/tasks/queue/BoundedBlockingQueue.java +++ b/src/main/java/hse/java/lectures/lecture6/tasks/queue/BoundedBlockingQueue.java @@ -1,25 +1,51 @@ package hse.java.lectures.lecture6.tasks.queue; -public class BoundedBlockingQueue { +import java.util.LinkedList; +public class BoundedBlockingQueue { + private final LinkedList list = new LinkedList<>(); + private int capacity; + private final Object monitor = new Object(); public BoundedBlockingQueue(int capacity) { - + if (capacity <= 0) { + throw new IllegalArgumentException("capacity must be positive"); + } + this.capacity = capacity; } - public void put(T item) { - + public void put(T item) throws InterruptedException { + if (item == null) { + throw new NullPointerException("item must be non-null"); + } + + synchronized (monitor) { + while (list.size() == capacity) { + monitor.wait(); + } + list.push(item); + notifyAll(); + } } - public T take() { - return null; + public T take() throws InterruptedException { + synchronized (monitor) { + while (list.isEmpty()) { + wait(); + } + T item = list.pop(); + notifyAll(); + return item; + } } public int size() { - return 0; + synchronized (monitor) { + return list.size(); + } } public int capacity() { - return 0; + return this.capacity; } } From 9ede7979d2e3ae1aa678f6878ac406eebd5afa3f Mon Sep 17 00:00:00 2001 From: Alexandr Baranov Date: Sat, 14 Mar 2026 13:21:22 +0300 Subject: [PATCH 2/3] queue: add tag, sync via this --- .../lecture6/tasks/queue/BoundedBlockingQueue.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/main/java/hse/java/lectures/lecture6/tasks/queue/BoundedBlockingQueue.java b/src/main/java/hse/java/lectures/lecture6/tasks/queue/BoundedBlockingQueue.java index a3af0b0d..c9aa926e 100644 --- a/src/main/java/hse/java/lectures/lecture6/tasks/queue/BoundedBlockingQueue.java +++ b/src/main/java/hse/java/lectures/lecture6/tasks/queue/BoundedBlockingQueue.java @@ -5,7 +5,6 @@ public class BoundedBlockingQueue { private final LinkedList list = new LinkedList<>(); private int capacity; - private final Object monitor = new Object(); public BoundedBlockingQueue(int capacity) { if (capacity <= 0) { @@ -19,9 +18,9 @@ public void put(T item) throws InterruptedException { throw new NullPointerException("item must be non-null"); } - synchronized (monitor) { + synchronized (this) { while (list.size() == capacity) { - monitor.wait(); + this.wait(); } list.push(item); notifyAll(); @@ -29,9 +28,9 @@ public void put(T item) throws InterruptedException { } public T take() throws InterruptedException { - synchronized (monitor) { + synchronized (this) { while (list.isEmpty()) { - wait(); + this.wait(); } T item = list.pop(); notifyAll(); @@ -40,7 +39,7 @@ public T take() throws InterruptedException { } public int size() { - synchronized (monitor) { + synchronized (this) { return list.size(); } } From ecedbbb25c84b98210aa5b9456e2b39541890076 Mon Sep 17 00:00:00 2001 From: Alexandr Baranov Date: Sat, 14 Mar 2026 13:37:51 +0300 Subject: [PATCH 3/3] queue: replace push/pop wiht addLast/removeFirst --- .../lecture6/tasks/queue/BoundedBlockingQueue.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/hse/java/lectures/lecture6/tasks/queue/BoundedBlockingQueue.java b/src/main/java/hse/java/lectures/lecture6/tasks/queue/BoundedBlockingQueue.java index c9aa926e..c335f5fd 100644 --- a/src/main/java/hse/java/lectures/lecture6/tasks/queue/BoundedBlockingQueue.java +++ b/src/main/java/hse/java/lectures/lecture6/tasks/queue/BoundedBlockingQueue.java @@ -8,7 +8,7 @@ public class BoundedBlockingQueue { public BoundedBlockingQueue(int capacity) { if (capacity <= 0) { - throw new IllegalArgumentException("capacity must be positive"); + throw new IllegalArgumentException("capacity must be positive"); } this.capacity = capacity; } @@ -22,7 +22,7 @@ public void put(T item) throws InterruptedException { while (list.size() == capacity) { this.wait(); } - list.push(item); + list.addLast(item); notifyAll(); } } @@ -32,7 +32,8 @@ public T take() throws InterruptedException { while (list.isEmpty()) { this.wait(); } - T item = list.pop(); + T item = list.getFirst(); + list.removeFirst(); notifyAll(); return item; }