public class ThreadTest守护线程 {
public static void main(String[] args) {
Thread t = new BakThread();
t.setName("备份线程");
//启动线程之前,将该线程设置为守护线程
t.setDaemon(true);
t.start();
//主线程:为用户线程
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName()+"--->"+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class BakThread extends Thread{
public void run(){
int i = 0;
while(true){
System.out.println(Thread.currentThread().getName()+"--->"+ ++i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
多线程的守护线程
最新推荐文章于 2022-09-08 20:42:33 发布