2011-01-12 45 views
0

我得到这个错误:的IsAlive problem..Help了解它是如何工作

"non-static method isAlive() cannot be referenced from a static context" 

什么不对的code..please。

我想检测是否该线程是活着... 中的代码会方面的任何帮助,高度appreciated..thanks 最大

class RecThread extends Thread { 

    public void run() { 

     recFile = new File("recorded_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); 

    } 
} 
+1

扩展线程通常不是这样一个好主意,请尝试使用Runnable。请使用rcOn的布尔变量。 – Thilo 2011-01-12 09:21:54

+1

* if(boolean == true)*不是很好阅读。人们往往更喜欢简单:* if(布尔值)* – Gugussee 2011-01-12 10:07:22

回答

2
if (RecThread.isAlive() == true) { 

这条线是有问题的。 isAlive()不是一个静态方法,这意味着它只能作用于Thread的一个实例。您通过使用类型(RecThread)而不是对象调用它来在静态上下文中使用它。

0

您正试图以静态方式访问非静态方法。我的意思是,如果运行当前实例的线程没有死,isAlive()方法会返回。

这就是为什么isAlive()不是静态的,它被绑定到实例(到线程的状态)而不是类本身。

0

从run()中检查isAlive()时没有意义。如果它正在执行run()中的代码,则表示它是活动的 如果某个其他线程具有RecThread对象,则该线程可以使用recThread。 isAlive()查看它是否仍在运行