0

I would like to make that OptionPanel makes threads shutdown themselves. I know it suppose to be done in the run method of the thread class but I don't know if I should to synchronize something there or not.

How to make that these threads will shut themselves after clicking correct option in JOptionPanel?

import javax.swing.JOptionPane;

public class Wat extends Thread {
    private char c;
    private int interv;
    private volatile boolean running = true;

    public Wat(char c, int interv) {
        this.c = c;
        this.interv = interv;
    }

    public void run() {

        while (running) {
            try {
                System.out.println(c);
                Thread.sleep(interv * 100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    public static void main(String[] args) {
        Wat w1 = new Wat('A', 3);
        Wat w2 = new Wat('B', 4);
        Wat w3 = new Wat('C', 5);
        w1.start();
        w2.start();
        w3.start();
        Object[] options = { "Shutdown A", "Shutdown B", "Shutdown C" };
        int option;
        while (w1.isAlive() || w2.isAlive() || w3.isAlive()) {
            option = JOptionPane.showOptionDialog(null,
                    "Which one would you like to shut?", "Threads",
                    JOptionPane.YES_NO_CANCEL_OPTION,
                    JOptionPane.QUESTION_MESSAGE, null, options, options[2]);
            switch (option) {
            case JOptionPane.YES_OPTION:
                w1.running = false;
                break;
            case JOptionPane.NO_OPTION:
                w2.running = false;
                break;
            case JOptionPane.CANCEL_OPTION:
                w3.running = false;
                break;
            }
        }

    }

}
Robert
  • 233
  • 3
  • 12
  • partial duplicate of http://stackoverflow.com/questions/5562720/how-to-access-the-running-thread-runnable. use the technique there. – Martin Serrano Nov 05 '12 at 17:53
  • Here's information about how to interrupt a thread: http://stackoverflow.com/questions/9791361/thread-not-interrupting/9791479#9791479 – Gray Nov 05 '12 at 17:53
  • I have edited my question is the code is correct now? Can it be done better? – Robert Nov 05 '12 at 23:47

1 Answers1

2

Given that you have:

Thread.sleep(interv * 100);

I would look at sending an interrupt to each thread. The interrupt will do just that to the sleep() method and it will be more responsive than the usual practise of looping around a boolean (e.g. while (!done) {....}).

As well as the tutorial linked above, check out this DeveloperWorks article on handling InteruptedExceptions.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • Isnt it considered bad practice to use Exceptions for control flow? – Brady Nov 05 '12 at 19:24
  • This interupt method did not work for me. I have edited my question is the code is correct now? Can it be done better? – Robert Nov 05 '12 at 23:46
  • @Brady - I wouldn't really class this as flow control. Besides, how would you interrupt a thread waiting on a socket or similar without such a mechanism. – Brian Agnew Nov 06 '12 at 08:23
  • A condition variable is a common mechanism to signal a thread, but then you would need some sort of while loop, which isnt necessarily bad. Its also quite common not to block waiting on a socket, but rather to call select() with a time-out value. – Brady Nov 06 '12 at 08:32