.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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());
|
||||
|
||||
Reference in New Issue
Block a user