java 之定时器设置

上一篇 / 下一篇  2017-05-05 18:00:53 / 个人分类:selenium自动化测试

问题:需要定时执行补推订单的任务,需要每天一次
解决方法:通过查询,发现* ScheduledExecutorService是从Java SE5的java.util.concurrent里,做为并发工具类被引进的,这是最理想的定时任务实现方式。  
  1. * 1>相比于Timer的单线程,它是通过线程池的方式来执行任务的
  2. * 2>可以很灵活的去设定第一次执行任务delay时间
  3. * 3>提供了良好的约定,以便设定执行的时间间隔

实现代码如下:

importjava.util.concurrent.Executors;

importjava.util.concurrent.ScheduledExecutorService;

importjava.util.concurrent.TimeUnit;

public static void main(String args[]){
   Runnable runnable = new Runnable() { 
    public void run() {
   //定义查询语句
     String queryString = "SELECT josoninfo from jd_back_order_insert_record WHERE returninfo  not LIKE '%listDate%' AND returninfo not LIKE '%success%' and returninfo <>''  and  create_time >='"+Creat_time+"'";
     Log.info(queryString);
     //查询得到returnInfo的数组
     List<String> returnInfoList = getreturnInfo(driverName,driverUrl,UserName,PassWord,queryString );
     Log.info("共获取数据库记录数:"+returnInfoList.size());
     //截取字符串“order_Info=”得到真正的json字符串数组
     List<String> returnJsonStringArray = getOrderjson(returnInfoList);
     //分析json值得到orderNum
     List<String> rderListOrderNum = getOrderIdFromJson(returnJsonStringArray);
     try {
    AddOrderList(OrderListOrderNum);
    Log.info("执行日期:"+date.formatDateTime(new Date())+"执行结束");
   } catch (Exception e) {
    // TODO Auto-generated catch block
    Log.error("调用addorderlist函数插入订单号异常", e);
   }
   
     Creat_time = date.formatDateTime(new Date());
     } 
  };
 
  ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor(); 
 // 第二个参数为首次执行的延时时间,第三个参数为定时执行的间隔时间 
 service.scheduleAtFixedRate(runnable, 1, 1, TimeUnit.DAYS); 
  } 
另外两种方法:
  1. /**
  2. * 普通thread
  3. * 这是最常见的,创建一个thread,然后让它在while循环里一直运行着,
  4. * 通过sleep方法来达到定时任务的效果。这样可以快速简单的实现,代码如下:
  5. * @author GT
  6. *
  7. */
  8. publicclassTask1 {
  9. publicstaticvoidmain(String[] args) {
  10. // run in a second
  11. finallongtimeInterval =1000;
  12. Runnable runnable =newRunnable() {
  13. publicvoidrun() {
  14. while(true) {
  15. // ------- code for task to run
  16. System.out.println("Hello !!");
  17. // ------- ends here
  18. try{
  19. Thread.sleep(timeInterval);
  20. }catch(InterruptedException e) {
  21. e.printStackTrace();
  22. }
  23. }
  24. }
  25. };
  26. Thread thread =newThread(runnable);
  27. thread.start();
  28. }
  29. importjava.util.Timer;
  30. importjava.util.TimerTask;
  31. /**
  32. *
  33. * 于上面方式相比,优势 1>当启动和去取消任务时可以控制 2>第一次执行任务时可以指定你想要的delay时间
  34. *
  35. * 在实现时,Timer类可以调度任务,TimerTask则是通过在run()方法里实现具体任务。 Timer实例可以调度多任务,它是线程安全的。
  36. * 当Timer的构造器被调用时,它创建了一个线程,这个线程可以用来调度任务。 下面是代码:
  37. *
  38. * @author GT
  39. *
  40. */
  41. publicclassTask2 {
  42. publicstaticvoidmain(String[] args) {
  43. TimerTask task =newTimerTask() {
  44. @Override
  45. publicvoidrun() {
  46. // task to run goes here
  47. System.out.println("Hello !!!");
  48. }
  49. };
  50. Timer timer =newTimer();
  51. longdelay =0;
  52. longintevalPeriod =1*1000;
  53. // schedules the task to be run in an interval
  54. timer.scheduleAtFixedRate(task, delay, intevalPeriod);
  55. }// end of main
  56. }   


参考地址http://blog.csdn.net/haorengoodman/article/details/23281343/


TAG: java 定时器

 

评分:0

我来说两句

Open Toolbar