1
0
This commit is contained in:
2024-09-22 15:42:52 +03:00
parent f06789d6de
commit 468750152c
3 changed files with 65 additions and 10 deletions

View File

@@ -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.
*
* <p>This class extends {@code Thread} and overrides the {@code run} method
* to calculate the integral when the thread starts running.</p>
*
* <p>Once the calculation is complete, the result can be retrieved using
* the {@link #getResult()} method.</p>
*
* @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<Double, Double> 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<Double, Double> 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;
}

View File

@@ -94,7 +94,7 @@ 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);

View File

@@ -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<Double, Double> function, double lowerBound, double upperBound) {
if (command.length < 2) {
System.out.println("Usage: start <accuracy>");
System.out.println("Usage: start <e>");
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 <threadId>");
System.out.println("Usage: stop <n>");
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 <threadId>");
System.out.println("Usage: await <n>");
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());