public class ThreadTest {
public static int sign= 0;
public static void main(String[] args) {
Thread thread = new Thread(
new Runnable(){
public void run(){
while(true){
if(sign%10 !=0){ //这个条件可以设置为取时间判断
System.out.println("守护线程保持运行");
sign++;
}else{
System.out.println("调用定时任务");
sign++;
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
);
thread.setDaemon(true); // 设置为守护线程,降低线程的优先级
thread.start();
while(true){
System.out.println("主线程保持运行"); //守护线程必须保证有主线程存在,才会生存。
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}