改错public class Parasitism extends Thread { public Parasitism(String name){ super(name); } public void run() { if(Thread.currentThread().isDaemon()) System.out.println(getName() + " is Daemon thread"); else System.out.println(getName() + " is User thread"); } public static void main(String[] args) { Parasitism t1 = new Parasitism("t1"); Parasitism t2 = new Parasitism("t2"); t1.start(); t2.start(); t1.setDaemon(true); }
时间: 2024-01-30 18:03:41 浏览: 119
人教高中生物必修期末考试题及答案.docx
在这个代码中,需要注意的是 `t1.setDaemon(true);` 应该在 `t1.start();` 之前调用,否则会抛出 `IllegalThreadStateException` 异常,因为在线程启动后,就不能再设置它的守护线程属性了。所以正确的代码应该是:
```
public class Parasitism extends Thread {
public Parasitism(String name) {
super(name);
}
public void run() {
if (Thread.currentThread().isDaemon())
System.out.println(getName() + " is Daemon thread");
else
System.out.println(getName() + " is User thread");
}
public static void main(String[] args) {
Parasitism t1 = new Parasitism("t1");
Parasitism t2 = new Parasitism("t2");
t1.setDaemon(true);
t1.start();
t2.start();
}
}
```
阅读全文