.
This commit is contained in:
@@ -2,14 +2,56 @@ package ru.lionarius;
|
|||||||
|
|
||||||
import java.util.function.Function;
|
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 {
|
public class CalculationThread extends Thread {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The calculator used to perform the integration.
|
||||||
|
*/
|
||||||
private final IntegralCalculator calculator;
|
private final IntegralCalculator calculator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The function to be integrated.
|
||||||
|
*/
|
||||||
private final Function<Double, Double> function;
|
private final Function<Double, Double> function;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The lower bound of the integration interval.
|
||||||
|
*/
|
||||||
private final double lowerBound;
|
private final double lowerBound;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The upper bound of the integration interval.
|
||||||
|
*/
|
||||||
private final double upperBound;
|
private final double upperBound;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The result of the integration.
|
||||||
|
*/
|
||||||
private double result;
|
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) {
|
public CalculationThread(IntegralCalculator calculator, Function<Double, Double> function, double lowerBound, double upperBound) {
|
||||||
this.calculator = calculator;
|
this.calculator = calculator;
|
||||||
this.function = function;
|
this.function = function;
|
||||||
@@ -17,11 +59,20 @@ public class CalculationThread extends Thread {
|
|||||||
this.upperBound = upperBound;
|
this.upperBound = upperBound;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates the integral of the specified function over the given interval.
|
||||||
|
* This method is executed when the thread is started.
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
this.result = this.calculator.calculate(this.function, this.lowerBound, this.upperBound);
|
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() {
|
public double getResult() {
|
||||||
return this.result;
|
return this.result;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ public class IntegralCalculator {
|
|||||||
if (function == null)
|
if (function == null)
|
||||||
throw new NullPointerException("function cannot be null");
|
throw new NullPointerException("function cannot be null");
|
||||||
|
|
||||||
if (lowerBound == upperBound) {
|
if (lowerBound == upperBound) {
|
||||||
this.callProgressCallback(0, 1);
|
this.callProgressCallback(0, 1);
|
||||||
this.callProgressCallback(1, 1);
|
this.callProgressCallback(1, 1);
|
||||||
|
|
||||||
@@ -94,9 +94,9 @@ public class IntegralCalculator {
|
|||||||
|
|
||||||
var sum = (function.apply(lowerBound) + function.apply(upperBound)) / 2;
|
var sum = (function.apply(lowerBound) + function.apply(upperBound)) / 2;
|
||||||
for (var i = 1L; i < n; i++) {
|
for (var i = 1L; i < n; i++) {
|
||||||
if (Thread.interrupted())
|
if (Thread.currentThread().isInterrupted())
|
||||||
break;
|
break;
|
||||||
|
|
||||||
sum += function.apply(lowerBound + h * i);
|
sum += function.apply(lowerBound + h * i);
|
||||||
|
|
||||||
this.callProgressCallback(i, n);
|
this.callProgressCallback(i, n);
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ public class Main {
|
|||||||
var command = input.split(" ");
|
var command = input.split(" ");
|
||||||
|
|
||||||
if (command.length == 0) {
|
if (command.length == 0) {
|
||||||
System.out.println("No command entered.");
|
System.out.println("No command entered");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,9 +60,10 @@ public class Main {
|
|||||||
*/
|
*/
|
||||||
private static void handleStartCommand(String[] command, Function<Double, Double> function, double lowerBound, double upperBound) {
|
private static void handleStartCommand(String[] command, Function<Double, Double> function, double lowerBound, double upperBound) {
|
||||||
if (command.length < 2) {
|
if (command.length < 2) {
|
||||||
System.out.println("Usage: start <accuracy>");
|
System.out.println("Usage: start <e>");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
double accuracy = Double.parseDouble(command[1]);
|
double accuracy = Double.parseDouble(command[1]);
|
||||||
var thread = new CalculationThread(new IntegralCalculator(accuracy), function, lowerBound, upperBound);
|
var thread = new CalculationThread(new IntegralCalculator(accuracy), function, lowerBound, upperBound);
|
||||||
int id = currentId++;
|
int id = currentId++;
|
||||||
@@ -79,9 +80,10 @@ public class Main {
|
|||||||
*/
|
*/
|
||||||
private static void handleStopCommand(String[] command) {
|
private static void handleStopCommand(String[] command) {
|
||||||
if (command.length < 2) {
|
if (command.length < 2) {
|
||||||
System.out.println("Usage: stop <threadId>");
|
System.out.println("Usage: stop <n>");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int id = Integer.parseInt(command[1]);
|
int id = Integer.parseInt(command[1]);
|
||||||
var thread = threads.remove(id);
|
var thread = threads.remove(id);
|
||||||
if (thread == null) {
|
if (thread == null) {
|
||||||
@@ -100,15 +102,17 @@ public class Main {
|
|||||||
*/
|
*/
|
||||||
private static void handleAwaitCommand(String[] command) {
|
private static void handleAwaitCommand(String[] command) {
|
||||||
if (command.length < 2) {
|
if (command.length < 2) {
|
||||||
System.out.println("Usage: await <threadId>");
|
System.out.println("Usage: await <n>");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int id = Integer.parseInt(command[1]);
|
int id = Integer.parseInt(command[1]);
|
||||||
var thread = threads.remove(id);
|
var thread = threads.remove(id);
|
||||||
if (thread == null) {
|
if (thread == null) {
|
||||||
System.out.println("Thread " + id + " not found");
|
System.out.println("Thread " + id + " not found");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
thread.join();
|
thread.join();
|
||||||
System.out.println("Result: " + thread.getResult());
|
System.out.println("Result: " + thread.getResult());
|
||||||
|
|||||||
Reference in New Issue
Block a user