0

I am implementing a program which rtp audio to a peer computer and in the same time running a threads to get ping data. Once I click the start button, two method start almost in the same time. The problem is when I want to interrupt/exit the program, the ping data will not show the output but the threads is still running.

Start button

JButton startCap = new JButton("Start");
    startCap.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent act) {

                            go();//go() is the rtp audio method

                        Thread pinging = new Thread() {
                            public void run() {

                                PingTest();
                            }
                        };

                        pinging.setName("runPing");
                        pinging.start();

        }
    });

Exit button

JButton stopCap = new JButton("Exit");
    stopCap.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent act) {
                            //just for some checking, I know its not a good design
                            //I new at here
            if (sink != null) {
                sink.addDataSinkListener(new DataSinkListener() {
                    public void dataSinkUpdate(DataSinkEvent e) {
                        if(e instanceof EndOfStreamEvent)
                            sink.close();
                    }
                });
            }
            if (processor != null)
                processor.stop();
                            System.exit(-1);
                            //I think there no need to return, but still put at there
                            Thread.currentThread().interrupt();//preserve the message
                            pinging.currentThread().interrupt();
                            return;//
                            //Runtime.getRuntime().halt(0);
                            //pinging.stop();
                            }

    });

Some GUI code

public static void createAndShowGui() {
    JFrame frame = new JFrame("Sender");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(new SimpleVoiceTransmiter());
    frame.setSize(200, 75);
    frame.setVisible(true);
    frame.toFront();
}

public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGui();
        }
    });
}

The go() is the main method while PingTest() is the dependent method, when go() start by clicking the "Start" button PingTest() will start. Now the problems is I don't know how to interupt/exit the PingTest(). How can I interrupt/exit all the threads (PingTest()) in the same time? Really need some hints, thanks in advanced.

p/s: PingTest() is using Runtime.exec() to ping the peer computer.

1 Answer 1

1

pinging.currentThread() doesn't return the pinging thread. It's equivalent to Thread.currentThread(), since currentThread() is a static method. To get the pinging thread, just use pinging:

pinging.interrupt();

I don't really understand why you would interrupt the current thread either. The current thread is the event dispatch thread, which is running your GUI. There is no reason to interrupt it.

1
  • Thanks, maybe I get the wrong concept about the currentThread() which make me thinks it will terminate/interrupt all the running threads in the program. I will try the pinging.interrupt(), thanks...
    – 薛源少
    Mar 3, 2013 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.