Java 定时器

什么是Java定时器?

Java 定时器就是在给定的间隔时间执行自己的任务; Java实现定时器有以下几种:

通过sleep 实现定时器(尽量不要用,这是使CPU睡眠,并且还霸占着CPU)

public class App {
	public static void main(String[] args) throws InterruptedException
	{
		boolean start = true;
		while(start)
		{
			System.out.println("==========无限循环,那我停1s吧!");
			Thread.sleep(1000);
		}
	}
}

通过 Timer 来实现定时任务

Timer 是来自 java.util.Timer,

  • 指定时间执行任务

    public class App {
    
    	public static void main(String[] args) throws InterruptedException
    	{
    		Timer timer = new Timer();
    		Date date = new Date(System.currentTimeMillis() + 5000);
    		timer.schedule(new TimerTask() {
    			public void run() {
    				System.out.println("run task = " + new Date());
    			}
    		}, date);
    
    		System.out.println("begin schedule : " +  date);
    	}
    }
    

    运行结果为

    begin schedule : Wed Sep 09 12:13:46 CST 2020
    run task = Wed Sep 09 12:13:46 CST 2020
    
    • 延后执行,随后以固定时间执行任务
    public class App {
    	public static void main(String[] args) throws InterruptedException
    	{
        Timer timer = new Timer();
    		Date date = new Date();
    		timer.schedule(new TimerTask() {
    			@Override
    			public void run() {
    				// TODO Auto-generated method stub
    				System.out.println("run task : " +  new Date());
    			}
    		}, 3000, 1000);
    		System.out.println("begin schedule : " +  date);
    
        // 取消任务,停止运行
        // timer.cancel();
    	}
    }
    
    参数说明
    
    1. 3000 表示 3s,在这里表示延后3s执行,
    2. 1000 表示 1s,在这里表示每隔1s执行1次
    3. timer.cancel();  取消任务,停止运行
    

    运行结果

    begin schedule : Wed Sep 09 12:23:00 CST 2020
    run task : Wed Sep 09 12:23:03 CST 2020
    run task : Wed Sep 09 12:23:04 CST 2020
    run task : Wed Sep 09 12:23:05 CST 2020
    run task : Wed Sep 09 12:23:06 CST 2020
    run task : Wed Sep 09 12:23:07 CST 2020
    run task : Wed Sep 09 12:23:08 CST 2020
    run task : Wed Sep 09 12:23:09 CST 2020
    run task : Wed Sep 09 12:23:10 CST 2020
    
  • 指定第一次运行时间点,并随后以固定时间周期执行任务

public class App {
	public static void main(String[] args) throws InterruptedException
	{
		Timer timer = new Timer();
		Date date = new Date();
		Date firstDate = new Date(System.currentTimeMillis() + 3000);
		timer.scheduleAtFixedRate(new TimerTask() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				System.out.println("run task : " +  new Date());
			}
		}, firstDate, 1000);

		System.out.println("begin schedule : " +  date);

		// 取消任务,停止运行
		// timer.cancel();
	}
}

代码解释

1. Date firstDate = new Date(System.currentTimeMillis() + 3000); 定义了第一次执行时间

2. }, firstDate, 1000); 中的 1000 表示 每隔1s执行任务!

运行结果如下

begin schedule : Wed Sep 09 12:25:45 CST 2020
run task : Wed Sep 09 12:25:48 CST 2020
run task : Wed Sep 09 12:25:49 CST 2020
run task : Wed Sep 09 12:25:50 CST 2020
run task : Wed Sep 09 12:25:51 CST 2020
run task : Wed Sep 09 12:25:52 CST 2020
run task : Wed Sep 09 12:25:53 CST 2020
run task : Wed Sep 09 12:25:54 CST 2020

通过线程池执行定时任务

public class App {
	public static void main(String[] args) throws InterruptedException
	{
		ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);
		pool.scheduleAtFixedRate(new Runnable() {
			@Override
			public void run() {
				System.out.println("====date = " + new Date());
			}
		}, 1, 1, TimeUnit.SECONDS);
		// 停止运行
		// pool.shutdown();

	}
}

代码说明:

TimeUnit.SECONDS 表示指定时间单位为 秒

运行结果为

====date = Wed Sep 09 12:46:51 CST 2020
====date = Wed Sep 09 12:46:52 CST 2020
====date = Wed Sep 09 12:46:53 CST 2020
====date = Wed Sep 09 12:46:54 CST 2020
====date = Wed Sep 09 12:46:55 CST 2020
====date = Wed Sep 09 12:46:56 CST 2020
====date = Wed Sep 09 12:46:57 CST 2020

定时器框架

Java 定时器框架也有很多, 如

  • Spring
  • QuartZ 支持cron表达式

有的时候Java代码定时任务也会让Linux上执行,也就是 Linux Cron