开发者

thread checking problem

开发者 https://www.devze.com 2023-02-03 17:02 出处:网络
I do need help. I\'m wracking my brain in order to understand how threads work. I\'m wondering what\'s wrong with this code?

I do need help. I'm wracking my brain in order to understand how threads work.

I'm wondering what's wrong with this code? My application has two threads. One thread starts when the user press button A, the other one starts when the B button is chosen. If one button is enabled the other one is disabled.

Now I'd like to be able to warn the user that a thread is still running whenever he/she tries to exit the program.

the code is....actually even though nothing is running the user is unable to exit.. Thank you very much

exitAction = new AbstractAction("Exit") {

        public void actionPerformed(ActionEvent e) {

          Threa开发者_如何学编程d thRunning = Thread.currentThread();


        if (thRunning.isAlive()) {

JOptionPane.showMessageDialog(ProgAu.this, "Be careful.Press STOP before exiting", "Process running", JOptionPane.WARNING_MESSAGE);

    }
     else {

            System.exit(0);}

        }
    };

-- This is one of the thread I'm refering to.... What should I do in order to detect its behaviour?

class RecThread extends Thread {

    public void run() {

        recFile = new File("rec.wav");

        AudioFileFormat.Type fileType = null;
        fileType = AudioFileFormat.Type.WAVE;



        try {

            targetDataLine.open(audioFormat);


            targetDataLine.start();


            AudioSystem.write(new AudioInputStream(targetDataLine),

fileType, recFile);

        } catch (Exception e) {
            showException(e);
        }


        playAction.setEnabled(true);
        stopPlayAction.setEnabled(false);
  recAction.setEnabled(true);
        stopRecAction.setEnabled(false);  




    }
}


What's wrong with the code is that it will always warn the user. Look at this:

Thread thRunning = Thread.currentThread();

That means you're looking at the currently running thread. The one that's executing that method call, and continuing to execute actionPerformed. How could that possibly not be running? It's like someone asking out loud whether the person who is speaking is dead.

When you start off your other thread (wherever that is) you need to keep a reference to that thread... that's the one you need to test for liveness. Or alternatively (and preferably IMO) the background threads could keep increment some counter when they start, and then decrement it when they end - you can only exit if the counter is 0. That way, if your design changes to allow multiple simultaneous background threads, you don't need to change your exit-checking code. Don't forget to perform the increment/decrement in a thread-safe way (e.g. using AtomicInteger) and make sure the decrement is within a finally block, so that the counter will still be decremented even if the thread throws an exception which isn't caught.


Will Thread.currentThread() not be referring to your GUI thread? Therefore it will always be alive.


The current thread is always alive. If it were dead it would be running the code to check if it was alive. Perhaps you intended to save the other thread in a field and check if it was alive?


Thread.CurrentThread().IsAlive() ALWAYS returns true, because you're interrogating the state of the thread on which you're running the code doing the checking. You'll need to keep a handle to the worker threads when you create them, and interrogate THEIR state in this method.


This checks, if the current thread (the one that executes the lines) is alive.

You could as well ask yourself: "Am I alive?" and the answer is clear (if you are not a zombie).

0

精彩评论

暂无评论...
验证码 换一张
取 消