Hi everyone,
Take a look at the following code block.

->Class InterruptionDialog
public class InterruptionDialog extends JDialog
{
      //Fields
      Thread currentThread;
      JButton interruptionButton;
      
      //Constructor
      public InterruptionDialog(JFrame parent, boolean modal, Thread t)
      {
           super(parent, modal);
           currentThread = t;
           interruptionButton = new JButton("Click to interrupt");
           interruptionButton.addActionListener( new ActionListener(){ 
                        public void ActionPerformed(Event evt) {
                            <????>
                   }});
       }
}

-> Class MyClass
public class MyClass
{
        //Fields
       Thread executionThread;
       ....
 
       public void criticalMethod()
       {
         executionThread = new Thread( new Runnable(){ 
           public void run(){
             InterruptionDialog d = new InterruptionDialog(null, false, executionThread);

             javax.swing.SwingUtilities.invokeLater( new Runnable(){ public void run(){
                       d.setVisible(true);  }});
               <STUFF HERE....>
             }});
             executionThread.start();
             
             try{
                executionTread.join();
             }
             catch(Exception e){}
            <STUFF HERE....>
       }
}

Where <????> is placed I want to stop the thread's execution upon user's request. I' ve used method t.stop(), which worked fine, but is deprecated. Is there any "non- deprecated" way to stop the execution of the Thread through the InterruptionDialog? I've read Sun's instructions but I haven't got anything useful out of it (or obviously I am as smart to understand it!). I would gladly provide more information if needed.
Thank you in advance,
F.

Don't use the deprecated methods. You simply set the thread to null, and that will stop it from running.

currentThread = null;

No, that won't stop the thread.
It will remove the reference to the thread but won't stop the process.

What you do is set a flag in the Runnable from outside the process and check for that. If the flag is set, terminate the run method.

for example, if t is the flag (not the Thread of course). Using a boolean is of course more natural, or maybe an int if you want to have more than 2 states.

This article has been dead for over six months. Start a new discussion instead.