当前位置:首页 > 开发 > 编程语言 > 编程 > 正文

Timer、TimerTask实现定时业务处理

发表于: 2013-03-14   作者:boonya   来源:转载   浏览次数:
摘要: 1、编写任务 package com.boonya.timer; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimerTask; /** * myTask.java * 功能:编写定时执行的任务 * @author boonya * @version 1.0 2
1、编写任务
package com.boonya.timer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimerTask;
/**
 * myTask.java
 * 功能:编写定时执行的任务
 * @author boonya
 * @version 1.0 2012-10-11
 */
public class myTask extends TimerTask{

	@Override
	public void run() {
		// TODO Auto-generated method stub
		Date mydate=new Date();
		String runTime=new SimpleDateFormat("yyy-MM-dd hh:mm:ss").format(mydate);
		System.out.println(runTime+"定时任务正在执行.........");
	}
}

2、设置调用简单任务
package com.boonya.timer;
import java.util.Timer;
/**
 * RunMyTask.java
 * 功能:调用定时执行的任务
 * @author boonya
 * @version 1.0 2012-10-11
 */
public class RunMyTask {
	
	public static void execute(){
		Timer timer = new Timer();
		//一天执行一次
		/*Date   start=new   Date();
	       start.setDate(start.getDate()+1);
	       start.setHours(2);
	       start.setMinutes(0);
	       start.setSeconds(0);
	       long l = start.getTime()-System.currentTimeMillis();
	       timer.schedule(new myTask(), l, 24*1000*60*60);*/
		 //1分钟执行一次的任务
		timer.schedule(new myTask(), 60000, 60*1000);//task,delay,peroid
	}
}

3、编写复杂任务调度策略的范围接口
public interface SchedulerService {
    void schedule(TimerTask task, long delay);
    void schedule(TimerTask task, long delay, long period);
    void schedule(TimerTask task, Date firstTime, long period);
    void scheduleAtFixedRate(TimerTask task, long delay, long period);
    void scheduleAtFixedRate(TimerTask task, Date firstTime, long period);
} 

4、实现任务策略接口

public class SchedulerServiceImpl implements SchedulerService {
    //延迟多长时间调用一次就陷入线程等待
    @Override
    public void schedule(TimerTask task, long delay) {
        new Timer().schedule(task, delay);
    }
   //延迟delay微秒调用一次,以后每隔period微秒执行一次
    @Override
    public void schedule(TimerTask task, long delay, long period) {
        new Timer().schedule(task, delay, period);
    }
   //从firstTime开始执行,每隔period微秒执行一次
    @Override
    public void schedule(TimerTask task, Date firstTime, long period) {
        new Timer().scheduleAtFixedRate(task, firstTime, period);
    }
   //延迟delay微秒调用一次,以后每隔period微秒执行一次:方法重在执行频率
    @Override
    public void scheduleAtFixedRate(TimerTask task, long delay, long period) {
        new Timer().scheduleAtFixedRate(task, delay, period);
    }
   //从firstTime开始执行,每隔period微秒执行一次:方法重在执行频率
    @Override
    public void scheduleAtFixedRate(TimerTask task, Date firstTime, long period) {
        new Timer().scheduleAtFixedRate(task, firstTime, period);
    }
} 
//注:建议只使用schedule方法。 

5、调度业务调用
public static void main(String[] args) {
        SchedulerService service=new SchedulerServiceImpl();
        // execute one time then the thread turn to wait status
        service.schedule(new TemplateTask(), 5*1000);
        // start from now every 5 seconds execute my task
//        service.schedule(new TemplateTask(), new Date(), 5*1000); 
        //delay 5 seconds then as 5 seconds time-step to execute my task
//        service.schedule(new TemplateTask(), 5*1000, 5*1000);
//        service.scheduleAtFixedRate(new TemplateTask(), new Date(), 5*1000);
//        service.scheduleAtFixedRate(new TemplateTask(), 5*1000, 5*1000);
} 

6、web配置项目启动执行任务
---可以设置一个Servlet在项目启动时启动定时任务,web.xml配置如下:
  
 <servlet>
      <description>This is the description of my J2EE component</description>
      <display-name>This is the display name of my J2EE component</display-name>
      <servlet-name>ReadUserListHandler</servlet-name>
      <servlet-class>com.boonya.test.servlet.ReadUserListHandler</servlet-class>
      <load-on-startup>5</load-on-startup>  
    </servlet>
    
     <servlet-mapping>
       <servlet-name>ReadUserListHandler</servlet-name>
       <url-pattern>/servlet/ReadUserListHandler</url-pattern>
    </servlet-mapping>
  注: <load-on-startup>说明,
  在servlet的配置当中,<load-on-startup>5</load-on-startup>的含义是:
  标记容器是否在启动的时候就加载这个servlet。
  当值为0或者大于0时,表示容器在应用启动时就加载这个servlet;
  当是一个负数时或者没有指定时,则指示容器在该servlet被选择时才加载。
  正数的值越小,启动该servlet的优先级越高。
7、注意事项
      每一个Timer仅对应唯一一个线程。
  Timer不保证任务执行的十分精确。
  Timer存在线程安全隐患。

Timer、TimerTask实现定时业务处理

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

extttttex target="_blyle="b_001_e="oaipt type="textextextttl> f" / style="b_001_iv bdis5:left;cipt type="textextexttt bsp; Tim276902 $(fuexttttt target="_blyle="b_001_c*t>
extttttex target="_blyle="b_001_e="oaipt type="textextextttl> f" / style="b_001_iv bdis6:left;cipt type="textextexttt bsp; Tim6"_b收 行的)学习e/8463.htm"}); })lass="reco 行的)学tm" ipt type="textextexttt 言extttttextttttexttttipt type="textextext/javascript"> $(fuexttttt target="_blyle="b_001_c*t>
extttttex target="_blyle="b_001_e="oaipt type="textextextttl> f" / style="b_001_iv bdis7:left;cipt type="textextexttt bsp; Tim758724 href=r />e/8463.htm"}); })disiss="recommend4">href=r />tm" ipt type="textextexttt 言extttttextttttexttttipt type="textextext/javascript"> $(fuexttttt target="_blyle="b_001_c*t>
extttttex target="_blyle="b_001_e="oaipt type="textextextttl> f" / style="b_001_iv bdis8:left;cipt type="textextexttt bsp; Tim99052 $(fuexttttt target="_blyle="b_001_c*t> extttttex target="_blyle="b_001_e="oaipt type="textextextttl> f" / style="b_001_iv bdis9:left;cipt type="textextexttt bsp; Tim9362"> $(f程安/article/2 .com/image/pe/8463.htm"}); })/article/2 .com/image/ptm" ipt type="textextexttt 言extttttextttttexttttipt type="textextext/javascript"> $(fuexttttt target="_blyle="b_001_c*t> extttttex target="_blyle="b_001_e="oaipt type="textextextttl> f" / style="b_001_iv bdis1t:left;cipt type="textextexttt bsp; Tim969022 $(fuexttttt target="_blyle="b_001_c*t> extttttex target="_blyle="b_001_e="oaipt type="textextextttl> nff" / style="b_001_iv bdis11:left;cipt type="textextexttt bsp; Tim1556 行的)学习e/8463.htm"}); })lass="reco 行的)学tm" ipt type="textextexttt 言extttttextttttexttttipt type="textextext/javascript"> $(fuexttttt target="_blyle="b_001_c*t> extttttex target="_blyle=ch_001_e="oaipt type="textextextttl> f" / style="b_001_iv bdis12:left;cipt type="textextexttt bsp; Tim5 > $(f程安lass="reco 行的)学习e/8463.htm"}); })lass="reco 行的)学tm" ipt type="textextexttt 言extttttextttttexttttipt type="textextext/javascript"> $(fuexttttt target="_blyle="b_001_c*t> extttttex target="_blyle="b_001_e="oaipt type="textextextttl> f" / style="b_001_iv bdis13:left;cipt type="textextexttt bsp; Tim104347 href=r />e/8463.htm"}); })disiss="recommend4">href=r />tm" ipt type="textextexttt 言extttttextttttexttttipt type="textextext/javascript"> $(fuexttttt target="_blyle="b_001_c*t> extttttex target="_blyle="b_001_e="oaipt type="textextextttl> f" / style="b_001_iv bdis14:left;cipt type="textextexttt bsp; Tim195821 e/8463.htm"}); })/article/2tm" ipt type="textextexttt 言extttttextttttexttttipt type="textextext/javascript"> $(fuexttttt target="_blyle="b_001_c*t> extttttex target="_blyle="b_001_e="oaipt type="textextextttl> f" / style="b_001_iv bdis15:left;cipt type="textextexttt bsp; Tim25 34 .com/image/p/b0c89ae
.com/image/p/b0c89tm" ipt type="textextexttt 言extttttextttttexttttipt type="textextext/javascript"> $(fuexttttt target="_blyle="b_001_c*t>
extttttex target="_blyle="b_001_e="oaipt type="textextextttl> f" / style="b_001_iv bdis16:left;cipt type="textextexttt bsp; Tim2 >95 $(fuexttttt target="_blyle="b_001_c*t> extttttex target="_blyle="b_001_e="oaipt type="textextextttl> f" / style="b_001_iv bdis17:left;cipt type="textextexttt bsp; Tim72974 代码之ss="re代码之ss="re $(fuexttttt target="_blyle="b_001_c*t> extttttex target="_blyle="b_001_e="oaipt type="textextextttl> f" / style="b_001_iv bdis18:left;cipt type="textextexttt bsp; Tim5903 > $(f程安jodhref=&行的 meta越髉ng&00/j+&quo项目启e/8463.htm"}); })jodhref=&行的 meta越髉tm" ipt type="textextexttt 言extttttextttttexttttipt type="textextext/javascript"> $(fuexttttt target="_blyle="b_001_c*t> extttttex target="_blyle="b_001_e="oaipt type="textextextttl> f" / style="b_001_iv bdis19:left;cipt type="textextexttt bsp; Tim270697 码之ss="re码之ss="re $(fuexttttt target="_blyle="b_001_c*t> extttttex target="_blyle="b_001_e="oaipt type="textextextttl> f" / style="b_001_iv bdis2t:left;cipt type="textextexttt bsp; Tim2006 e/8463.htm"}); })href=与href=r />tm" ipt type="textextexttt 言extttttextttttexttttipt type="textextext/javascript"> $(fuext/javascript"> $(f/javascript"> /javascrip.htm" .js" t程章m.js?v=89860 pt t < *、TimerT任s="scom" src="/r/cms/www/itf" / st 04t">
$(fa target="_blank" href="http://www.pt /script> target="_blyle="b_002_c*t> extttttex target="_blyle="b_002_e="oaipt type="textextextttl> f" / style="b_002_iv bdis1:left;cipt type="textextexttt bsp; Tim" > extttttex target="_blyle="b_002_e="oaipt type="textextextttl> f" / style="b_002_iv bdis2:left;cipt type="textextexttt bsp; Tim" > extttttex target="_blyle="b_002_e="oaipt type="textextextttl> f" / style="b_002_iv bdis3:left;cipt type="textextexttt bsp; Tim" >
$(fuexttttt target="_blyle="b_002_c*t>
extttttex target="_blyle="b_002_e="oaipt type="textextextttl> f" / style="b_002_iv bdis4:left;cipt type="textextexttt bsp; Tim1 eAtFix $(fuexttttt target="_blyle="b_002_c*t> extttttex target="_blyle="b_002_e="oaipt type="textextextttl> f" / style="b_002_iv bdis5:left;cipt type="textextexttt bsp; Tim2 extttttex target="_blyle="b_002_e="oaipt type="textextextttl> f" / style="b_002_iv bdis6:left;cipt type="textextexttt bsp; Tim249757 $(fuexttttt target="_blyle="b_002_c*t> extttttex target="_blyle="b_002_e="oaipt type="textextextttl> f" / style="b_002_iv bdis7:left;cipt type="textextexttt bsp; Tim212115 .com/ima&nbsstrong> ;ort 言extttttextttttexttttipt type="textextext/javascript"> $(fuexttttt target="_blyle="b_002_c*t> extttttex target="_blyle="b_002_e="oaipt type="textextextttl> f" / style="b_002_iv bdis8:left;cipt type="textextexttt bsp; Tim1694" > $(fuexttttt target="_blyle="b_002_c*t> extttttex target="_blyle="b_002_e="oaipt type="textextextttl> f" / style="b_002_iv bdis9:left;cipt type="textextexttt bsp; Tim2520862 $(fuexttttt target="_blyle="b_002_c*t> extttttex target="_blyle="b_002_e="oaipt type="textextextttl> f" / style="b_002_iv bdis1t:left;cipt type="textextexttt bsp; Tim2345424 $(fu 言ipt type= 言ipt t 言i pt t < *、TimerT任s="sc~(-new Date()/36e5)];
/栏_begin --t pt t
/ab Str <>s="e们tm" -t pt t < SetHos(2ef="#" locaegin)" fi&nb Sc">取消" -t pt t <
-> 2 <>站内 tm" -t pt t <
->le="ap">Sle="aptm" ipt t 言ipt t < *、TimerT任版权所 spa识> ©h1>09-1>T5 spa识> s Re个ed. pt t->