I am writing a GUI for a program that takes some inputs and runs an algorithm on them. The code for the algorithm is quite long and complex, so I just started a new thread from the GUI to do the calculations on the inputs.
if(e.getSource() == startButton) {
if(update.updateStrings(textFields)) {
algorithmThread.start();
}
}
We want to add functionality that will allow the user to stop the calculation (it works for about half an hour on my laptop until I get the result) in case they provided the wrong input files. This is how I deal with this.
else if(e.getSource() == stopButton) {
algorithmThread.interrupt();
System.out.println("algorithm stopped");
algorithmThread = new Thread() {
public void run() {
runNOC();
}
};
}
( , ), . : Thread.interrupted() ? / ? , ?