From 317ad4f7a4a729dc68c78a669ee06ae80dd2e457 Mon Sep 17 00:00:00 2001 From: Daniil Khanukov Date: Sun, 16 Nov 2025 15:14:35 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D0=92=D1=8B=D0=BF=D0=BE=D0=BB=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=B7=D0=B0=D0=B4=D0=B0=D1=87=D0=B8=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../spring/myworks/RingBuffer.java | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/main/java/com/daniilkhanukov/spring/myworks/RingBuffer.java diff --git a/src/main/java/com/daniilkhanukov/spring/myworks/RingBuffer.java b/src/main/java/com/daniilkhanukov/spring/myworks/RingBuffer.java new file mode 100644 index 0000000..f0d3464 --- /dev/null +++ b/src/main/java/com/daniilkhanukov/spring/myworks/RingBuffer.java @@ -0,0 +1,88 @@ +package com.daniilkhanukov.spring.myworks; + +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; + +public class RingBuffer { + private final int[] buffer; + private final int capacity; + private int head = 0; + private int tail = 0; + private int size = 0; + + public RingBuffer(int capacity) { + this.capacity = capacity; + buffer = new int[capacity]; + } + + private final Lock lock = new ReentrantLock(); + private final Condition notEmpty = lock.newCondition(); + private final Condition notFull = lock.newCondition(); + + public void put(int value) throws InterruptedException { + lock.lock(); + try { + while (size == capacity) { + notFull.await(); + } + buffer[tail] = value; + tail = (tail + 1) % capacity; + size++; + notEmpty.signal(); + } finally { + lock.unlock(); + } + } + + public int get() throws InterruptedException { + lock.lock(); + try { + while (size == 0) { + notEmpty.await(); + } + int value = buffer[head]; + head = (head + 1) % capacity; + size--; + notFull.signal(); + return value; + } finally { + lock.unlock(); + } + } + + public static void main(String[] args) throws InterruptedException { + RingBuffer ringBuffer = new RingBuffer(5); + + Thread producer = new Thread(() -> { + try { + for (int i = 0; i < 23; i++) { + ringBuffer.put(i); + System.out.println("Вставлено значение " + i); + Thread.sleep(1000); + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + }); + + Thread consumer = new Thread(() -> { + try { + for (int i = 0; i < 23; i++) { + int item = ringBuffer.get(); + System.out.println("Получено значение: " + item); + Thread.sleep(2500); + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + }); + + producer.start(); + consumer.start(); + + producer.join(); + consumer.join(); + System.out.println("Конец"); + } +} From 85aaab228384f8d6a7fee3cadc2149544f1d9ad1 Mon Sep 17 00:00:00 2001 From: Daniil Khanukov Date: Tue, 28 Apr 2026 23:07:55 +0300 Subject: [PATCH 2/2] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F=20=D1=81=D0=BE=D0=BD=D0=B0=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/daniilkhanukov/spring/myworks/RingBuffer.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/daniilkhanukov/spring/myworks/RingBuffer.java b/src/main/java/com/daniilkhanukov/spring/myworks/RingBuffer.java index f0d3464..0995f65 100644 --- a/src/main/java/com/daniilkhanukov/spring/myworks/RingBuffer.java +++ b/src/main/java/com/daniilkhanukov/spring/myworks/RingBuffer.java @@ -1,5 +1,8 @@ package com.daniilkhanukov.spring.myworks; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; @@ -16,6 +19,8 @@ public RingBuffer(int capacity) { buffer = new int[capacity]; } + private static final Logger log = LoggerFactory.getLogger(RingBuffer.class); + private final Lock lock = new ReentrantLock(); private final Condition notEmpty = lock.newCondition(); private final Condition notFull = lock.newCondition(); @@ -58,7 +63,7 @@ public static void main(String[] args) throws InterruptedException { try { for (int i = 0; i < 23; i++) { ringBuffer.put(i); - System.out.println("Вставлено значение " + i); + log.info("Вставлено значение {}", i); Thread.sleep(1000); } } catch (InterruptedException e) { @@ -70,7 +75,7 @@ public static void main(String[] args) throws InterruptedException { try { for (int i = 0; i < 23; i++) { int item = ringBuffer.get(); - System.out.println("Получено значение: " + item); + log.info("Получено значение: {}", item); Thread.sleep(2500); } } catch (InterruptedException e) { @@ -83,6 +88,6 @@ public static void main(String[] args) throws InterruptedException { producer.join(); consumer.join(); - System.out.println("Конец"); + log.info("Конец"); } }