守护线程
package xiancheng2;
/*
* 守护线程:
* 其他所有的用户线程结束,则守护线程退出!
* 守护线程一般都是无限执行的
*/
public class ThreadTest4 {
public static void main(String[] args) throws InterruptedException {
Thread t1=new Proccessor();
t1.setName("t1");
//将t1用户线程修改成守护线程
t1.setDaemon(true);
t1.start();
//主线程
for(int i=0;i<10 ;i++) {
System.out.println(Thread.currentThread().getName()+"-->"+i);
Thread.sleep(1000);
}
}
}
class Proccessor extends Thread{
public void run() {
int i=0;
while (true) {
i=i+1;
System.out.println(Thread.currentThread().getName()+"-->"+i);
try {
Thread.sleep(500);
} catch (Exception e) {
// TODO: handle exception
}
}
}
}