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

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);
	}

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

posted @ 2017-09-08 14:40  南方有嘉木1993  阅读(276)  评论(0编辑  收藏  举报