From 468750152caedd7857e4ecacd1ad62c1d0881a56 Mon Sep 17 00:00:00 2001 From: lionarius Date: Sun, 22 Sep 2024 15:42:52 +0300 Subject: [PATCH] . --- .../java/ru/lionarius/CalculationThread.java | 57 ++++++++++++++++++- .../java/ru/lionarius/IntegralCalculator.java | 6 +- src/main/java/ru/lionarius/Main.java | 12 ++-- 3 files changed, 65 insertions(+), 10 deletions(-) diff --git a/src/main/java/ru/lionarius/CalculationThread.java b/src/main/java/ru/lionarius/CalculationThread.java index 248d316..23538c5 100644 --- a/src/main/java/ru/lionarius/CalculationThread.java +++ b/src/main/java/ru/lionarius/CalculationThread.java @@ -2,14 +2,56 @@ package ru.lionarius; import java.util.function.Function; +/** + * The {@code CalculationThread} class represents a thread that performs + * numerical integration over a specific interval using a given function. + * It utilizes the {@link IntegralCalculator} to compute the integral for + * the specified bounds of the function. + * + *

This class extends {@code Thread} and overrides the {@code run} method + * to calculate the integral when the thread starts running.

+ * + *

Once the calculation is complete, the result can be retrieved using + * the {@link #getResult()} method.

+ * + * @see IntegralCalculator + */ public class CalculationThread extends Thread { + + /** + * The calculator used to perform the integration. + */ private final IntegralCalculator calculator; + + /** + * The function to be integrated. + */ private final Function function; + + /** + * The lower bound of the integration interval. + */ private final double lowerBound; + + /** + * The upper bound of the integration interval. + */ private final double upperBound; - + + /** + * The result of the integration. + */ private double result; - + + /** + * Constructs a new {@code CalculationThread} with the specified calculator, + * function, and integration bounds. + * + * @param calculator the {@link IntegralCalculator} used to compute the integral + * @param function the function to integrate, represented as a {@link Function} + * @param lowerBound the lower bound of the integration interval + * @param upperBound the upper bound of the integration interval + */ public CalculationThread(IntegralCalculator calculator, Function function, double lowerBound, double upperBound) { this.calculator = calculator; this.function = function; @@ -17,11 +59,20 @@ public class CalculationThread extends Thread { this.upperBound = upperBound; } + /** + * Calculates the integral of the specified function over the given interval. + * This method is executed when the thread is started. + */ @Override public void run() { this.result = this.calculator.calculate(this.function, this.lowerBound, this.upperBound); } - + + /** + * Returns the result of the integration after the thread completes execution. + * + * @return the computed result of the integral + */ public double getResult() { return this.result; } diff --git a/src/main/java/ru/lionarius/IntegralCalculator.java b/src/main/java/ru/lionarius/IntegralCalculator.java index 282d4b9..b71babf 100644 --- a/src/main/java/ru/lionarius/IntegralCalculator.java +++ b/src/main/java/ru/lionarius/IntegralCalculator.java @@ -71,7 +71,7 @@ public class IntegralCalculator { if (function == null) throw new NullPointerException("function cannot be null"); - if (lowerBound == upperBound) { + if (lowerBound == upperBound) { this.callProgressCallback(0, 1); this.callProgressCallback(1, 1); @@ -94,9 +94,9 @@ public class IntegralCalculator { var sum = (function.apply(lowerBound) + function.apply(upperBound)) / 2; for (var i = 1L; i < n; i++) { - if (Thread.interrupted()) + if (Thread.currentThread().isInterrupted()) break; - + sum += function.apply(lowerBound + h * i); this.callProgressCallback(i, n); diff --git a/src/main/java/ru/lionarius/Main.java b/src/main/java/ru/lionarius/Main.java index 88c62ab..929cd58 100644 --- a/src/main/java/ru/lionarius/Main.java +++ b/src/main/java/ru/lionarius/Main.java @@ -29,7 +29,7 @@ public class Main { var command = input.split(" "); if (command.length == 0) { - System.out.println("No command entered."); + System.out.println("No command entered"); continue; } @@ -60,9 +60,10 @@ public class Main { */ private static void handleStartCommand(String[] command, Function function, double lowerBound, double upperBound) { if (command.length < 2) { - System.out.println("Usage: start "); + System.out.println("Usage: start "); return; } + double accuracy = Double.parseDouble(command[1]); var thread = new CalculationThread(new IntegralCalculator(accuracy), function, lowerBound, upperBound); int id = currentId++; @@ -79,9 +80,10 @@ public class Main { */ private static void handleStopCommand(String[] command) { if (command.length < 2) { - System.out.println("Usage: stop "); + System.out.println("Usage: stop "); return; } + int id = Integer.parseInt(command[1]); var thread = threads.remove(id); if (thread == null) { @@ -100,15 +102,17 @@ public class Main { */ private static void handleAwaitCommand(String[] command) { if (command.length < 2) { - System.out.println("Usage: await "); + System.out.println("Usage: await "); return; } + int id = Integer.parseInt(command[1]); var thread = threads.remove(id); if (thread == null) { System.out.println("Thread " + id + " not found"); return; } + try { thread.join(); System.out.println("Result: " + thread.getResult());