Do I need to use thread.interrupted ()?

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.

//algorithmThread previously initialized 
 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) {
        //if the user presses the stop button then intterupt the 
        //thread running the algorithm
        algorithmThread.interrupt();
        System.out.println("algorithm stopped"); //debugging code
        //recreate the thread in case the user hits the start button again
        algorithmThread = new Thread() {
                public void run() {
                    runNOC();
                }
            };
    }

( , ), . : Thread.interrupted() ? / ? , ?

+4
2

Thread.interrupt() "", . , , , . Java concurrency .

, , SwingWorker. , , , GUI, done, Thread.interrupt(). - , , InterruptedException , , Thread.sleep Object.wait.

+4

interrupt , : Thread.interrupt() ?

: InterruptedExceptions. , :

try {
     // Some code that might throw an InterruptedException.  
     // Using sleep as an example
     Thread.sleep(10000); 
     } 
catch (InterruptedException ie) {
     System.err.println("Interrupted in our long run.  Stopping.");
     Thread.currentThread().interrupt(); 
     } 

:

  • . auto-exception IDE - ie.printStackTrace(); jaunty "TODO: - !" .

  • . , , a InterruptedException, .

0

All Articles