1
0

better exit

This commit is contained in:
2024-09-23 17:58:47 +03:00
parent 468750152c
commit 55773a4713

View File

@@ -6,6 +6,11 @@ import java.util.Scanner;
import java.util.function.Function; import java.util.function.Function;
public class Main { public class Main {
/**
* Flag to indicate whether the application is running or not.
*/
private static boolean isRunning = true;
/** /**
* Counter to assign unique IDs to each calculation thread. * Counter to assign unique IDs to each calculation thread.
*/ */
@@ -23,7 +28,7 @@ public class Main {
var scanner = new Scanner(System.in); var scanner = new Scanner(System.in);
while (true) { while (isRunning) {
System.out.println("Enter desired command (start <e>, stop <n>, await <n>, exit):"); System.out.println("Enter desired command (start <e>, stop <n>, await <n>, exit):");
var input = scanner.nextLine().trim(); var input = scanner.nextLine().trim();
var command = input.split(" "); var command = input.split(" ");
@@ -129,6 +134,14 @@ public class Main {
private static void handleExitCommand() { private static void handleExitCommand() {
System.out.println("Shutting down..."); System.out.println("Shutting down...");
threads.values().forEach(CalculationThread::interrupt); threads.values().forEach(CalculationThread::interrupt);
System.exit(0);
for (var thread : threads.values()) {
try {
thread.join();
} catch (InterruptedException ignored) {
}
}
isRunning = false;
} }
} }