isAlive problem..Help to understand how it works
I get this error:
"non-static method isAlive() cannot be referenced from a static context"
what's wrong with this code..please.
I'd like to detect if the thread is alive... Any help in terms of code will be highly appreciated..thanks max
class RecThread extends Thread { public void run() { recFile = new File("rec开发者_JAVA技巧orded_track.wav"); // Output file type AudioFileFormat.Type fileType = null; fileType = AudioFileFormat.Type.WAVE; // if rcOn =1 thread is alive int rcOn; try { // starts recording targetDataLine.open(audioFormat); targetDataLine.start(); AudioSystem.write(new AudioInputStream(targetDataLine), fileType, recFile); if (RecThread.isAlive() == true) { rcOn =1; } else { rcOn =0; } } catch (Exception e) { showException(e); } // update actions recAction.setEnabled(true); stopRecAction.setEnabled(false); } }
if (RecThread.isAlive() == true) {
this line is problematic. isAlive() is not a static method, which means it can only act on an instance of a Thread. you are using it in a static context by calling it with a type (RecThread) instead of an object.
You are trying to access in a static way to a method which is not static. I mean, isAlive() method returns if the thread which is running the current instance is not dead.
That's why isAlive() is not static, it is bounded to an instance (to the state of the thread) not to the class itself.
No meaning in checking isAlive() from within run()..If it is executing code in run() it means it is live If some other thread has an object of RecThread, then that thread can use recThread.isAlive() to see if its still running
精彩评论