Java教程

java中定时器模拟数据库备份

本文主要是介绍java中定时器模拟数据库备份,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
package dingShiTask;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class TimerTest {
    public static void main(String[] args) throws Exception{
        //创建定时器对象
        Timer timer = new Timer();
        //Timer timer = new Timer(true);//守护线程的方式

        //指定定时任务
        //timer.schedule(定时任务,第一次执行时间,间隔多久执行一次);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date firstTime = sdf.parse("2021-3-17 11:24:30");
        timer.schedule(new LogTimerTask(),firstTime,1000);
    }
}
//编写一个类
//假设这是一个记录日志的定时任务
class LogTimerTask extends TimerTask{
    @Override
    public void run() {
        //编写需要执行的任务就行了。
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strTime = sdf.format(new Date());
        System.out.println(strTime +":成功完成第一次数据备份!");
    }
}

  

这篇关于java中定时器模拟数据库备份的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!