用线程实现简单的定时扫描任务

public static void test() {
		
		final long timeInterval = 60 * 60 * 1000;
		
		Runnable runnable = new Runnable() {
			public void run() {
				PECFileCaptureTask tPECFileCaptureTask = new PECFileCaptureTask();
				while(tPECFileCaptureTask.getReceiveFileCount() < 1) {
					try {
						System.out.println("安全。");
						Thread.sleep(timeInterval);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				while(tPECFileCaptureTask.getReceiveFileCount() >= 1) {
					try {
						System.out.println("危险!");
						Thread.sleep(timeInterval);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		};
		
		ScheduledExecutorService service =Executors.newSingleThreadScheduledExecutor(); 
		service.scheduleAtFixedRate(runnable, 0, 1, TimeUnit.SECONDS);
	}

项目启动时调用这个方法,启动线程,就可以每隔一小时扫描一次。

原文地址:https://www.cnblogs.com/dubinyang/p/12129935.html