From 1d015981867a83cfd68e8b2c048ccb0e352beb27 Mon Sep 17 00:00:00 2001 From: Matvey Bazhenov Date: Sat, 14 Mar 2026 13:10:01 +0300 Subject: [PATCH 1/3] queue: test --- .../tasks/queue/BoundedBlockingQueue.java | 52 +++++++++++++++---- 1 file changed, 42 insertions(+), 10 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..bdbeab10 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,57 @@ package hse.java.lectures.lecture6.tasks.queue; -public class BoundedBlockingQueue { - - - public BoundedBlockingQueue(int capacity) { +import java.util.LinkedList; +import java.util.Queue; +public class BoundedBlockingQueue { + private final Queue q = new LinkedList(); + private int size = 0; + private final int capacity; + private final Object monitor; + + public BoundedBlockingQueue(int capacity) throws InterruptedException { + if (capacity > 0) { + this.capacity = capacity; + monitor = new Object(); + } else { + throw new InterruptedException("Capacity is zero"); + } } - public void put(T item) { - + public void put(T item) throws InterruptedException { + if (item == null) { + throw new NullPointerException("Item is null"); + } + synchronized (monitor) { + while (size == capacity) { + monitor.wait(); + } + q.add(item); + size++; + monitor.notifyAll(); + } } - public T take() { - return null; + public T take() throws InterruptedException { + synchronized (monitor) { + while (size == 0) { + monitor.wait(); + } + T value = q.remove(); + size--; + monitor.notifyAll(); + return value; + } } public int size() { - return 0; + synchronized (monitor){ + monitor.notify(); + return size; + } } public int capacity() { - return 0; + return capacity; } } From 10cfcc30c0bac3b5523014be8e6ea77d0035041e Mon Sep 17 00:00:00 2001 From: Matvey Bazhenov Date: Sat, 14 Mar 2026 13:15:40 +0300 Subject: [PATCH 2/3] queue: exceptions --- .../tasks/queue/BoundedBlockingQueue.java | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 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 bdbeab10..37e5e67a 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 @@ -9,22 +9,26 @@ public class BoundedBlockingQueue { private final int capacity; private final Object monitor; - public BoundedBlockingQueue(int capacity) throws InterruptedException { + public BoundedBlockingQueue(int capacity) { if (capacity > 0) { this.capacity = capacity; monitor = new Object(); } else { - throw new InterruptedException("Capacity is zero"); + throw new IllegalArgumentException("Capacity is zero"); } } - public void put(T item) throws InterruptedException { + public void put(T item) { if (item == null) { throw new NullPointerException("Item is null"); } synchronized (monitor) { while (size == capacity) { - monitor.wait(); + try { + monitor.wait(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } } q.add(item); size++; @@ -32,10 +36,14 @@ public void put(T item) throws InterruptedException { } } - public T take() throws InterruptedException { + public T take() { synchronized (monitor) { while (size == 0) { - monitor.wait(); + try { + monitor.wait(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } } T value = q.remove(); size--; @@ -45,7 +53,7 @@ public T take() throws InterruptedException { } public int size() { - synchronized (monitor){ + synchronized (monitor) { monitor.notify(); return size; } From 6e8965559067056feb05ffc92ad6f0020d296047 Mon Sep 17 00:00:00 2001 From: Matvey Bazhenov Date: Sat, 14 Mar 2026 13:19:41 +0300 Subject: [PATCH 3/3] queue: fix exception --- .../lecture6/tasks/queue/BoundedBlockingQueue.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 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 37e5e67a..b24cc150 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 @@ -18,7 +18,7 @@ public BoundedBlockingQueue(int capacity) { } } - public void put(T item) { + public void put(T item) throws InterruptedException { if (item == null) { throw new NullPointerException("Item is null"); } @@ -27,7 +27,7 @@ public void put(T item) { try { monitor.wait(); } catch (InterruptedException e) { - throw new RuntimeException(e); + throw e; } } q.add(item); @@ -36,13 +36,13 @@ public void put(T item) { } } - public T take() { + public T take() throws InterruptedException { synchronized (monitor) { while (size == 0) { try { monitor.wait(); } catch (InterruptedException e) { - throw new RuntimeException(e); + throw e; } } T value = q.remove();