java 定时任务
来源:互联网 发布:扣分清零淘宝 编辑:程序博客网 时间:2024/06/08 17:17
ScheduleExecutorService接口中有四个重要的方法,其中scheduleAtFixedRate和scheduleWithFixedDelay在实现定时程序时比较方便。
下面是该接口的原型定义
使用ScheduleExecutorService实现周期性及定点时间执行任务
import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.concurrent.Executors;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.TimeUnit;/** * 基于线程池设计的 ScheduledExecutor。其设计思想是, * 每一个被调度的任务都会由线程池中一个线程去执行,因 * 此任务是并发执行的,相互之间不会受到干扰。需 要注意 * 的是,只有当任务的执行时间到来时,ScheduedExecutor * 才会真正启动一个线程,其余时间 ScheduledExecutor 都 * 是在轮询任务的状态。 * @author Administrator * */public class Timer {//设置线程池个数private static final int POOLCOUNT=2;private static ScheduledExecutorService service;private static boolean flog=true;//操作执行代码private static void TimerMethod(){if(flog){flog=false;System.out.println("基于ScheduledExecutorService 定时任务");flog=true;}}//定时操作线程private static final Runnable timerTask=new Runnable() {public void run() {TimerMethod();}};//定时任务public static void timerTask(){if(service==null){service= Executors.newScheduledThreadPool(POOLCOUNT);synchronized (service) {/* * Runnable command, 要执行的任务 * long initialDelay, 首次执行的延迟时间 * long period, 连续执行之间的周期 * TimeUnit unit 第二个和第三个参数的时间单位 *///首次执行延迟3秒 ,每5秒执行一次service.scheduleAtFixedRate(timerTask,1000*3, 1000*10, TimeUnit.MILLISECONDS);//定时执行time();}}}//终止定时器public static void stop(){synchronized (service) {if(service!=null && !service.isShutdown()){//关闭服务service.shutdown();System.out.println("关闭了。。。。。。。。。");service=null;}}}//定点时间执行 public static void time(){ // 24小时 long oneDay = 24 * 60 * 60 * 1000; //初始化时间 减去 系统当前时间 long initDelay = getTimeMillis("15:30:00") - System.currentTimeMillis(); initDelay = initDelay > 0 ? initDelay : oneDay + initDelay; service.scheduleAtFixedRate( new Runnable() { public void run() { System.out.println("每天15:30:00点执行了这个方法"); } }, initDelay,//首次执行延迟时间 oneDay, //每隔24小时执行一次 TimeUnit.MILLISECONDS);//参数单位毫秒 } //格式化时间 private static long getTimeMillis(String time) { try { //格式化日期 DateFormat dateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss"); DateFormat dayFormat = new SimpleDateFormat("yy-MM-dd"); //转换时间 long Date curDate = dateFormat.parse(dayFormat.format(new Date()) + " " + time); return curDate.getTime(); } catch (ParseException e) { e.printStackTrace(); } return 0; } public static void main(String[] args) {timerTask();try {Thread.sleep(1000*10);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}//stop();}}
使用 TimerTask
Timer和TimerTask是util包中两个与工作排程的类,Timer是计时器,可以设定 成特定时间或特定的时间周期产生信号,不过这里只有Timer是没有用的,必须配合TimerTask才有作用。Timer一旦与某个TimerTask 产生关联,就会在产生信号的同时,连带一起执行TimerTask所定义的工作。
TimerTask的实现只需要继承TimerTask类就并实现其run()方法就可以了。run()方法 是由我们自己来编写的,把你想做的工作放在里面,一旦Timer在特定时间内或周期产生信号,run()方法就会执行,我们通会Timer的 schdeule()方法来设定特定时间或特定的周期。schdeule()有两种形式,一个是两个参数的,一个是三个参数的。二种参数的第一个参数是 TimerTask的对象,第二个是时间也可是以Date对象。具有三个参数的schedule方法可以使一个task在某一个时间后,根据一定的间隔时 间运行多次,具有周期性。最后,可以使用Timer的cancel()方法来停止Timer,调用cancel()之后,两者就会脱离关系。 TimerTask本身也有cancel()方法。
import java.util.Timer;import java.util.TimerTask;public class TimerTasks {/* *注:TimerTask 实现的是runnable 接口 *Timer是一种定时器工具,用来在一个后台线程计划执行指定任务。它可以计划执行一个任务一次或反复多次。 *TimerTask一个抽象类,它的子类代表一个可以被Timer计划的任务。 *每一个Timer仅对应唯一一个线程。 *Timer不保证任务执行的十分精确。 *Timer类的线程安全的。 * *schedule(TimerTask task, long delay, long period) *schedule(TimerTask task, Date time, long period) *scheduleAtFixedRate(TimerTask task, long delay, long period) *scheduleAtFixedRate(TimerTask task, Date firstTime, long period) */ private static Timer t;private static boolean flog=true;//执行定时任务线程private static final TimerTask tsk=new TimerTask() {@Overridepublic void run() {//需要执行的任务if(flog){flog=false;System.out.println("基于TimerTask启用定时任务");flog=true;}}};//启动定时任务public synchronized static void timerTask(){/* * 第一个参数:TimerTask对象 * 第二个参数:开始执行第一个run方法时候延长的时间 * 第三个参数 每隔多少时间执行一次 */ if(t==null){t=new Timer();t.schedule(tsk, 1000*3, 1000*3);}}//终止任务public static void stop(){t.cancel();}public static void main(String[] args) {timerTask();try {Thread.sleep(10000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}stop();}}
编写监听器
import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;/** * * 编写监听器 * */public class Listener implements ServletContextListener {//web容器终止 加载contextDestroyed 方法public void contextDestroyed(ServletContextEvent sce) {Timer.stop();TimerTasks.stop();}//web容器启动时初始化contextInitialized 方法public void contextInitialized(ServletContextEvent sce) {Timer.timerTask();TimerTasks.timerTask();}}
<listener><listener-class>Listener</listener-class></listener>
0 0
- java定时任务,每天定时执行任务
- java定时任务,每天定时执行任务
- Java定时任务,每天定时执行任务
- java定时任务,每天定时执行任务
- javaweb 定时任务 java定时任务
- java定时任务,每天定时执行任务
- java定时任务,每天定时执行任务
- java定时任务,每天定时执行任务
- java定时任务,每天定时执行任务
- java定时任务,每天定时执行任务
- java定时任务,每天定时执行任务
- java定时任务 每天定时执行任务
- java定时任务,每天定时执行任务
- Java 定时任务 与 Spring定时任务
- java定时任务,每天定时执行任务
- java定时任务,每天定时执行任务
- java定时任务
- 定时任务实现 java
- [Elasticsearch] 部分匹配 (四) - 索引期间优化ngrams及索引期间的即时搜索
- [Hb-XVI] 编写包含多个功能子程序的中断例程(直接定址表)
- ActiveX数据对象之事务控制在VB和DELPHI中的应用
- 03base_syntax
- C++中三种正则表达式比较(C regex,C ++regex,boost regex)
- java 定时任务
- FastDFS原理系列文章
- 系统重启后,需要重新格式化HDFS问题解决方案
- Maven by Example 1.3. A Common Interface
- Linux及Arm-Linux程序开发笔记(零基础入门篇)
- mysql server 5.7 新建数据库连接报错(10061)
- 第17周项目4-日期结构体(2)生日差几天
- java中的Buffer抽象类
- 恩布企业IM 1.8 版本,企业内部通讯软件