当前位置: 首页 > news >正文

金属材料网站建设无锡网站建设上海韵茵

金属材料网站建设,无锡网站建设上海韵茵,wordpress安装后输入什么域名,wordpress证书关闭在我们编程过程中如果需要执行一些简单的定时任务#xff0c;无须做复杂的控制#xff0c;我们可以考虑使用JDK中的Timer定时任务来实现。下面LZ就其原理、实例以及Timer缺陷三个方面来解析java Timer定时器。 一、简介 在java中一个完整定时任务需要由Timer、TimerTask两个…在我们编程过程中如果需要执行一些简单的定时任务无须做复杂的控制我们可以考虑使用JDK中的Timer定时任务来实现。下面LZ就其原理、实例以及Timer缺陷三个方面来解析java Timer定时器。 一、简介 在java中一个完整定时任务需要由Timer、TimerTask两个类来配合完成。 API中是这样定义他们的Timer一种工具线程用其安排以后在后台线程中执行的任务。可安排任务执行一次或者定期重复执行。由TimerTaskTimer 安排为一次执行或重复执行的任务。我们可以这样理解Timer是一种定时器工具用来在一个后台线程计划执行指定任务而TimerTask一个抽象类它的子类代表一个可以被Timer计划的任务。 Timer类 在工具类Timer中提供了四个构造方法每个构造方法都启动了计时器线程同时Timer类可以保证多个线程可以共享单个Timer对象而无需进行外部同步所以Timer类是线程安全的。但是由于每一个Timer对象对应的是单个后台线程用于顺序执行所有的计时器任务一般情况下我们的线程任务执行所消耗的时间应该非常短但是由于特殊情况导致某个定时器任务执行的时间太长那么他就会“独占”计时器的任务执行线程其后的所有线程都必须等待它执行完这就会延迟后续任务的执行使这些任务堆积在一起具体情况我们后面分析。 当程序初始化完成Timer后定时任务就会按照我们设定的时间去执行Timer提供了schedule方法该方法有多中重载方式来适应不同的情况如下 schedule(TimerTask task, Date time)安排在指定的时间执行指定的任务。 schedule(TimerTask task, Date firstTime, long period) 安排指定的任务在指定的时间开始进行重复的固定延迟执行。 schedule(TimerTask task, long delay) 安排在指定延迟后执行指定的任务。 schedule(TimerTask task, long delay, long period) 安排指定的任务从指定的延迟后开始进行重复的固定延迟执行。 同时也重载了scheduleAtFixedRate方法scheduleAtFixedRate方法与schedule相同只不过他们的侧重点不同区别后面分析。 scheduleAtFixedRate(TimerTask task, Date firstTime, long period)安排指定的任务在指定的时间开始进行重复的固定速率执行。 scheduleAtFixedRate(TimerTask task, long delay, long period)安排指定的任务在指定的延迟后开始进行重复的固定速率执行。 TimerTask TimerTask类是一个抽象类由Timer 安排为一次执行或重复执行的任务。它有一个抽象方法run()方法该方法用于执行相应计时器任务要执行的操作。因此每一个具体的任务类都必须继承TimerTask然后重写run()方法。 另外它还有两个非抽象的方法 boolean cancel()取消此计时器任务。 long scheduledExecutionTime()返回此任务最近实际执行的安排执行时间。 二、实例 2.1、指定延迟时间执行定时任务 复制代码 public class TimerTest01 { Timer timer; public TimerTest01(int time){ timer new Timer(); timer.schedule(new TimerTaskTest01(), time * 1000); } public static void main(String[] args) { System.out.println(“timer begin…”); new TimerTest01(3); } } public class TimerTaskTest01 extends TimerTask{ public void run() { System.out.println(“Time’s up!!!”); } } 复制代码 运行结果 首先打印timer begin… 3秒后打印Time’s up!!! 2.2、在指定时间执行定时任务 复制代码 public class TimerTest02 { Timer timer; public TimerTest02(){ Date time getTime(); System.out.println(“指定时间time” time); timer new Timer(); timer.schedule(new TimerTaskTest02(), time); } public Date getTime(){ Calendar calendar Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 11); calendar.set(Calendar.MINUTE, 39); calendar.set(Calendar.SECOND, 00); Date time calendar.getTime(); /spanspan stylecolor: #0000ffreturn/spanspan stylecolor: #000000 time;} public static void main(String[] args) { new TimerTest02(); } } public class TimerTaskTest02 extends TimerTask{ Override public void run() { System.out.println(“指定时间执行线程任务…”); } } 复制代码 当时间到达11:39:00时就会执行该线程任务当然大于该时间也会执行执行结果为 指定时间timeTue Jun 10 11:39:00 CST 2014 指定时间执行线程任务… 2.3、在延迟指定时间后以指定的间隔时间循环执行定时任务 复制代码 public class TimerTest03 { Timer timer; public TimerTest03(){ timer new Timer(); timer.schedule(new TimerTaskTest03(), 1000, 2000); } public static void main(String[] args) { new TimerTest03(); } } public class TimerTaskTest03 extends TimerTask{ Override public void run() { Date date new Date(this.scheduledExecutionTime()); System.out.println(“本次执行该线程的时间为” date); } } 复制代码 运行结果: 复制代码 本次执行该线程的时间为Tue Jun 10 21:19:47 CST 2014 本次执行该线程的时间为Tue Jun 10 21:19:49 CST 2014 本次执行该线程的时间为Tue Jun 10 21:19:51 CST 2014 本次执行该线程的时间为Tue Jun 10 21:19:53 CST 2014 本次执行该线程的时间为Tue Jun 10 21:19:55 CST 2014 本次执行该线程的时间为Tue Jun 10 21:19:57 CST 2014 … 复制代码 对于这个线程任务,如果我们不将该任务停止,他会一直运行下去。 对于上面三个实例LZ只是简单的演示了一下同时也没有讲解scheduleAtFixedRate方法的例子其实该方法与schedule方法一样 2.4、分析schedule和scheduleAtFixedRate 1、schedule(TimerTask task, Date time)、schedule(TimerTask task, long delay) 对于这两个方法而言如果指定的计划执行时间scheduledExecutionTime systemCurrentTime则task会被立即执行。scheduledExecutionTime不会因为某一个task的过度执行而改变。 2、schedule(TimerTask task, Date firstTime, long period)、schedule(TimerTask task, long delay, long period) 这两个方法与上面两个就有点儿不同的前面提过Timer的计时器任务会因为前一个任务执行时间较长而延时。在这两个方法中每一次执行的task的计划时间会随着前一个task的实际时间而发生改变也就是scheduledExecutionTime(n1)realExecutionTime(n)periodTime。也就是说如果第n个task由于某种情况导致这次的执行时间过程最后导致systemCurrentTime scheduledExecutionTime(n1)这是第n1个task并不会因为到时了而执行他会等待第n个task执行完之后再执行那么这样势必会导致n2个的执行实现scheduledExecutionTime放生改变即scheduledExecutionTime(n2) realExecutionTime(n1)periodTime。所以这两个方法更加注重保存间隔时间的稳定。 3、scheduleAtFixedRate(TimerTask task, Date firstTime, long period)、scheduleAtFixedRate(TimerTask task, long delay, long period) 在前面也提过scheduleAtFixedRate与schedule方法的侧重点不同schedule方法侧重保存间隔时间的稳定而scheduleAtFixedRate方法更加侧重于保持执行频率的稳定。为什么这么说原因如下。在schedule方法中会因为前一个任务的延迟而导致其后面的定时任务延时而scheduleAtFixedRate方法则不会如果第n个task执行时间过长导致systemCurrentTime scheduledExecutionTime(n1)则不会做任何等待他会立即执行第n1个task所以scheduleAtFixedRate方法执行时间的计算方法不同于schedule而是scheduledExecutionTime(n)firstExecuteTime n*periodTime该计算方法永远保持不变。所以scheduleAtFixedRate更加侧重于保持执行频率的稳定。 三、Timer的缺陷 3.1、Timer的缺陷 Timer计时器可以定时指定时间执行任务、延迟延迟5秒执行任务、周期性地执行任务每隔个1秒执行任务但是Timer存在一些缺陷。首先Timer对调度的支持是基于绝对时间的而不是相对时间所以它对系统时间的改变非常敏感。其次Timer线程是不会捕获异常的如果TimerTask抛出的了未检查异常则会导致Timer线程终止同时Timer也不会重新恢复线程的执行他会错误的认为整个Timer线程都会取消。同时已经被安排单尚未执行的TimerTask也不会再执行了新的任务也不能被调度。故如果TimerTask抛出未检查的异常Timer将会产生无法预料的行为。 1、Timer管理时间延迟缺陷 前面Timer在执行定时任务时只会创建一个线程任务如果存在多个线程若其中某个线程因为某种原因而导致线程任务执行时间过长超过了两个任务的间隔时间会发生一些缺陷 复制代码 public class TimerTest04 { private Timer timer; public long start; public TimerTest04(){ this.timer new Timer(); start System.currentTimeMillis(); } public void timerOne(){ timer.schedule(new TimerTask() { public void run() { System.out.println(“timerOne invoked ,the time:” (System.currentTimeMillis() - start)); try { Thread.sleep(4000); //线程休眠3000 } catch (InterruptedException e) { e.printStackTrace(); } } }, 1000); } public void timerTwo(){ timer.schedule(new TimerTask() { public void run() { System.out.println(“timerOne invoked ,the time:” (System.currentTimeMillis() - start)); } }, 3000); } public static void main(String[] args) throws Exception { TimerTest04 test new TimerTest04(); test.timerOne(); test.timerTwo();} } 复制代码 按照我们正常思路timerTwo应该是在3s后执行其结果应该是 timerOne invoked ,the time:1001 timerOne invoked ,the time:3001 但是事与愿违timerOne由于sleep(4000)休眠了4S同时Timer内部是一个线程导致timeOne所需的时间超过了间隔时间结果 timerOne invoked ,the time:1000 timerOne invoked ,the time:5000 2、Timer抛出异常缺陷 如果TimerTask抛出RuntimeExceptionTimer会终止所有任务的运行。如下 复制代码 public class TimerTest04 { private Timer timer; public TimerTest04(){ this.timer new Timer(); } public void timerOne(){ timer.schedule(new TimerTask() { public void run() { throw new RuntimeException(); } }, 1000); } public void timerTwo(){ timer.schedule(new TimerTask() { /spanspan stylecolor: #0000ffpublic/span span stylecolor: #0000ffvoid/spanspan stylecolor: #000000 run() {System.out.println(/spanquot;我会不会执行呢quot;span stylecolor: #000000);} }, /span1000span stylecolor: #000000);} public static void main(String[] args) { TimerTest04 test new TimerTest04(); test.timerOne(); test.timerTwo(); } } 复制代码 运行结果timerOne抛出异常导致timerTwo任务终止。 Exception in thread “Timer-0” java.lang.RuntimeException at com.chenssy.timer.TimerTest04$1.run(TimerTest04.java:25) at java.util.TimerThread.mainLoop(Timer.java:555) at java.util.TimerThread.run(Timer.java:505) 对于Timer的缺陷我们可以考虑 ScheduledThreadPoolExecutor 来替代。Timer是基于绝对时间的对系统时间比较敏感而ScheduledThreadPoolExecutor 则是基于相对时间Timer是内部是单一线程而ScheduledThreadPoolExecutor内部是个线程池所以可以支持多个任务并发执行。 3.2、用ScheduledExecutorService替代Timer 1、解决问题一 复制代码 public class ScheduledExecutorTest { private ScheduledExecutorService scheduExec; public long start; ScheduledExecutorTest(){ this.scheduExec Executors.newScheduledThreadPool(2); this.start System.currentTimeMillis(); } public void timerOne(){ scheduExec.schedule(new Runnable() { public void run() { System.out.println(“timerOne,the time:” (System.currentTimeMillis() - start)); try { Thread.sleep(4000); } catch (InterruptedException e) { e.printStackTrace(); } } },1000,TimeUnit.MILLISECONDS); } public void timerTwo(){ scheduExec.schedule(new Runnable() { public void run() { System.out.println(“timerTwo,the time:” (System.currentTimeMillis() - start)); } },2000,TimeUnit.MILLISECONDS); } public static void main(String[] args) { ScheduledExecutorTest test new ScheduledExecutorTest(); test.timerOne(); test.timerTwo(); } } 复制代码 运行结果 timerOne,the time:1003 timerTwo,the time:2005 2、解决问题二 复制代码 public class ScheduledExecutorTest { private ScheduledExecutorService scheduExec; public long start; ScheduledExecutorTest(){ this.scheduExec Executors.newScheduledThreadPool(2); this.start System.currentTimeMillis(); } public void timerOne(){ scheduExec.schedule(new Runnable() { public void run() { throw new RuntimeException(); } },1000,TimeUnit.MILLISECONDS); } public void timerTwo(){ scheduExec.scheduleAtFixedRate(new Runnable() { public void run() { System.out.println(“timerTwo invoked …”); } },2000,500,TimeUnit.MILLISECONDS); } public static void main(String[] args) { ScheduledExecutorTest test new ScheduledExecutorTest(); test.timerOne(); test.timerTwo(); } } 运行结果 复制代码 timerTwo invoked … timerTwo invoked … timerTwo invoked … timerTwo invoked … timerTwo invoked … timerTwo invoked … timerTwo invoked … timerTwo invoked … timerTwo invoked …
http://www.hkea.cn/news/14257910/

相关文章:

  • 网站建设+开源wordpress 小米主题
  • 网站建设 排名宝下拉wordpress媒体文件隔离
  • 保定网站设计制作建筑网片重量计算公式
  • 我想在泉州做网站html怎么做静态网站
  • 信息查询类网站是怎么做的登录wordpress
  • 视频库网站建设茶文化建设网站的意义
  • dw网站开发环境搭建低价网站建设策划内容
  • 商城网站开发合同四川省城乡与建设厅网站首页
  • 电商设计网站有哪些功能网站流量来源查询
  • 网站建设亿码酷出名5一个域名能同时做2个网站吗
  • 网站排名查询alexa百度广告投放价格
  • 南沙网站建设wwiw义乌网站建设哪家好
  • 与网站建设相关的论文题目深圳自助建站
  • 怎么做卡盟网站wordpress做的网站效果
  • 网站开发方面知识深圳龙岗住房和建设局网站官网
  • 江西建设门户网站苍梧县网站建设
  • 工业电商做网站怎么样网页版梦幻西游天象攻略
  • 湛江建设免费网站网站维护意义
  • 太原建设局网站东风地区网站建设价格
  • 网站建设 书籍下载python编写网页
  • 网站建设总经理岗位职责营销案例100例小故事及感悟
  • 企业网站后台管理软件开发模型对比
  • cms网站是什么意思展厅设计平面布置图
  • 福州网站制作设计服务器调用wordpress
  • xuezuo网站建设软件开发培训学校软件开发培训机构
  • 广州市网站建设 乾图信息科技wordpress同步博客
  • 提供建站服务的网络公司的比较软件技术专业月薪多少
  • 学校网站源码phpphp网站文件夹结构
  • 如何在工商网站做预先核名效果图在线网
  • 影响网站pr的因素有哪些软件详细设计文档模板