我想出了这个简单的方法来获取正在运行的线程的状态,但我遇到了一些问题。如果有人可以帮助我修复它们或给我一种新的方法来做到这一点,那就太好了。这是我的线程类代码:
public class DLThread implements Runnable{
Thread dl;
String dlurl;
boolean running;
public DLThread(String cmd) {
dl = new Thread(this, "DL Thread");
dl.start();
dlurl = cmd.substring(3);
}
@Override
public void run() {
running = true;
System.out.println("Thread is Running");
try{
Methods.downloadExecute(dlurl);
running = false;
System.out.println("Thread done");
}
catch(Exception e){
e.printStackTrace();
running = false;
System.out.println("Thread done");
}
}
public boolean getState() {
return running;
}
}
我正在使用布尔“正在运行”来确定线程是否正在运行或是否已完成。这是我尝试调用它的地方:
if ( cmd.startsWith( "dl=" )) {
boolean running = DLThread.getState();
if(running){
new DLThread(cmd);
}
}
我收到此错误:non-static method getState() cannot be referenced from a static context