From 5dfe968b2f0842c7f72a567f21a0298a91a205d7 Mon Sep 17 00:00:00 2001 From: Mac Date: Sat, 14 Mar 2026 12:51:13 +0300 Subject: [PATCH 1/6] queue: first try --- .../tasks/queue/BoundedBlockingQueue.java | 36 ++++++++++++++++--- 1 file changed, 31 insertions(+), 5 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..32fc54ef 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; +import java.util.LinkedList; +import java.util.Queue; + public class BoundedBlockingQueue { + int capacity; + Queue queue = new LinkedList(); public BoundedBlockingQueue(int capacity) { + if (capacity <= 0){ + throw new IllegalArgumentException(); + } + this.capacity = capacity; } public void put(T item) { + if (item == null){ + throw new IllegalArgumentException(); + } + + while (queue.size() == capacity){ + try {wait();} catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + queue.offer(item); } - public T take() { - return null; + public synchronized T take() { + while (queue.isEmpty()){ + try {wait();} catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + T item = queue.poll(); + notifyAll(); + return item; } - public int size() { - return 0; + public synchronized int size() { + return queue.size(); } public int capacity() { - return 0; + return capacity; } } From d52909c02e2f7c3b53a49bf36ce05fee3fc8e997 Mon Sep 17 00:00:00 2001 From: Mac Date: Sat, 14 Mar 2026 12:59:04 +0300 Subject: [PATCH 2/6] queue: added synchronized --- .../lectures/lecture6/tasks/queue/BoundedBlockingQueue.java | 4 ++-- 1 file changed, 2 insertions(+), 2 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 32fc54ef..9fbdebbf 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 @@ -16,14 +16,14 @@ public BoundedBlockingQueue(int capacity) { this.capacity = capacity; } - public void put(T item) { + public synchronized void put(T item) { if (item == null){ throw new IllegalArgumentException(); } while (queue.size() == capacity){ try {wait();} catch (InterruptedException e) { - throw new RuntimeException(e); + e.printStackTrace(); } } From a5865cde1d1032e2de7e3dc383dd8e520fe8af4f Mon Sep 17 00:00:00 2001 From: Mac Date: Sat, 14 Mar 2026 13:01:13 +0300 Subject: [PATCH 3/6] queue: deleted try/catch block --- .../lecture6/tasks/queue/BoundedBlockingQueue.java | 8 ++------ 1 file changed, 2 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 9fbdebbf..f9508bcf 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 @@ -22,9 +22,7 @@ public synchronized void put(T item) { } while (queue.size() == capacity){ - try {wait();} catch (InterruptedException e) { - e.printStackTrace(); - } + wait(); } queue.offer(item); @@ -32,9 +30,7 @@ public synchronized void put(T item) { public synchronized T take() { while (queue.isEmpty()){ - try {wait();} catch (InterruptedException e) { - throw new RuntimeException(e); - } + wait(); } T item = queue.poll(); notifyAll(); From b74c80be74579fc1493f5e5d62f46acfade5a4cf Mon Sep 17 00:00:00 2001 From: Mac Date: Sat, 14 Mar 2026 13:04:11 +0300 Subject: [PATCH 4/6] queue: returned try/catch block --- .../lecture6/tasks/queue/BoundedBlockingQueue.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 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 f9508bcf..9654d4db 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 @@ -22,7 +22,11 @@ public synchronized void put(T item) { } while (queue.size() == capacity){ - wait(); + try { + wait(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } } queue.offer(item); @@ -30,7 +34,11 @@ public synchronized void put(T item) { public synchronized T take() { while (queue.isEmpty()){ - wait(); + try { + wait(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } } T item = queue.poll(); notifyAll(); From b9eacab65b6e054808fdf1cdf19a7cbb7b82614c Mon Sep 17 00:00:00 2001 From: Mac Date: Sat, 14 Mar 2026 13:05:55 +0300 Subject: [PATCH 5/6] queue: added notifyAll() in put(...) --- .../java/lectures/lecture6/tasks/queue/BoundedBlockingQueue.java | 1 + 1 file changed, 1 insertion(+) 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 9654d4db..35cef791 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 @@ -30,6 +30,7 @@ public synchronized void put(T item) { } queue.offer(item); + notifyAll(); } public synchronized T take() { From a9f18d3ed73c6b1a08c0c6056a868956fbe131c2 Mon Sep 17 00:00:00 2001 From: Mac Date: Sat, 14 Mar 2026 13:08:59 +0300 Subject: [PATCH 6/6] queue: added throws ..., delete try/catch block --- .../tasks/queue/BoundedBlockingQueue.java | 15 +++++---------- 1 file changed, 5 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 35cef791..79bffb6c 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 @@ -16,30 +16,25 @@ public BoundedBlockingQueue(int capacity) { this.capacity = capacity; } - public synchronized void put(T item) { + public synchronized void put(T item) throws InterruptedException { if (item == null){ throw new IllegalArgumentException(); } while (queue.size() == capacity){ - try { + wait(); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } + } queue.offer(item); notifyAll(); } - public synchronized T take() { + public synchronized T take() throws InterruptedException { while (queue.isEmpty()){ - try { + wait(); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } } T item = queue.poll(); notifyAll();