0

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.

4
Contributors
5
Replies
6
Views
12 Years
Discussion Span
Last Post by mmiikkee12
0

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

currentThread = null;

0

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.

0

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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.