zoukankan      html  css  js  c++  java
  • 用线程实现简单的定时扫描任务

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

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

  • 相关阅读:
    [Postman]历史(8)
    [Postman]响应(7)
    [Postman]请求(6)
    [Postman]查找替换(5)
    ORA-02050故障诊断一例
    转 js实践篇:例外处理Try{}catch(e){}
    转 PHP
    HTML DOM getElementById() 方法
    地点选择
    9i 和 11 g 区别
  • 原文地址:https://www.cnblogs.com/dubinyang/p/12129935.html
Copyright © 2011-2022 走看看