gpt4 book ai didi

java - isAlive 问题..帮助理解它是如何工作的

转载 作者:行者123 更新时间:2023-12-02 08:25:12 25 4
gpt4 key购买 nike

我收到此错误:

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

这段代码有什么问题..请。

我想检测线程是否还活着...代码方面的任何帮助将不胜感激..谢谢最大

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);

}
}

最佳答案

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

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

关于java - isAlive 问题..帮助理解它是如何工作的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4666968/

25 4 0