From 55219bf62c353a3269d88e7e19ccc1fa3d7c138f Mon Sep 17 00:00:00 2001 From: Daniil Khanukov Date: Thu, 13 Nov 2025 21:44:41 +0300 Subject: [PATCH] =?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=201?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../spring/myworks/MyWorksApplication.java | 17 ++++- .../spring/myworks/NumberPrinter.java | 72 +++++++++++++++++++ 2 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/daniilkhanukov/spring/myworks/NumberPrinter.java diff --git a/src/main/java/com/daniilkhanukov/spring/myworks/MyWorksApplication.java b/src/main/java/com/daniilkhanukov/spring/myworks/MyWorksApplication.java index 045628b..21efecd 100644 --- a/src/main/java/com/daniilkhanukov/spring/myworks/MyWorksApplication.java +++ b/src/main/java/com/daniilkhanukov/spring/myworks/MyWorksApplication.java @@ -6,8 +6,21 @@ @SpringBootApplication public class MyWorksApplication { - public static void main(String[] args) { - SpringApplication.run(MyWorksApplication.class, args); + public static void main(String[] args) throws InterruptedException { + + int N = 13; + NumberPrinter printer = new NumberPrinter(N); + + Thread tEven = new Thread(printer::printEven); + Thread tOdd = new Thread(printer::printOdd); + + tEven.start(); + tOdd.start(); + + tEven.join(); + tOdd.join(); + +// SpringApplication.run(MyWorksApplication.class, args); } } diff --git a/src/main/java/com/daniilkhanukov/spring/myworks/NumberPrinter.java b/src/main/java/com/daniilkhanukov/spring/myworks/NumberPrinter.java new file mode 100644 index 0000000..dd63bcd --- /dev/null +++ b/src/main/java/com/daniilkhanukov/spring/myworks/NumberPrinter.java @@ -0,0 +1,72 @@ +package com.daniilkhanukov.spring.myworks; + +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.ReentrantLock; + +public class NumberPrinter { + + private final int max; + private int current = 0; + + private final ReentrantLock lock = new ReentrantLock(); + private final Condition condition = lock.newCondition(); + private boolean isEvenNow = true; + + /** + * + * @param max до указанного числа невключительно + */ + public NumberPrinter(int max) { + this.max = max; + } + + public void printEven() { + try { + while (true) { + lock.lock(); + try { + while (current < max && !isEvenNow) { + condition.await(); + } + if (current >= max) { + condition.signalAll(); + break; + } + System.out.println("Чётный поток: " + current); + current++; + isEvenNow = false; + condition.signalAll(); + } finally { + lock.unlock(); + } + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + + public void printOdd() { + try { + while (true) { + lock.lock(); + try { + while (current < max && isEvenNow) { + condition.await(); + } + if (current >= max) { + condition.signalAll(); + return; + } + System.out.println("Нечётный поток: " + current); + current++; + isEvenNow = true; + condition.signalAll(); + } finally { + lock.unlock(); + } + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } +}