正如前面提到的,我们可以将一个线程作为守护线程,并在检测其他线程状态时执行一些检测代码吗?我希望守护程序线程每5分钟执行一次(或多或少,不准确度不大,可以接受几秒延迟)
答案 0 :(得分:0)
看看这个:
public static void main(String[] args) throws InterruptedException {
ScheduledThreadPoolExecutor svc = new ScheduledThreadPoolExecutor(1, new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t1 = new Thread(r);
t1.setDaemon(true);
return t1;
}
});
svc.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
System.out.println("Himanshu "+Thread.currentThread().getName());
}
}, 5, 5, TimeUnit.SECONDS);
svc.awaitTermination(10000, TimeUnit.SECONDS);
}
答案 1 :(得分:0)