JAVA精确定时器

上传者: rediculous | 上传时间: 2021-09-22 12:05:51 | 文件大小: 18KB | 文件类型: ZIP
JAVA精确定时器,利用系统时间,使长期工作的误差稳定。 功能: ·可定时启动任务或直接启动任务 ·重复启动任务(时间间隔可在任务线程中改变,范围大于100ms,否则精度降低) 引用列表: ·import psn.razerpen.thread.AccuracyTimer; ·import psn.razerpen.thread.AccuracyTimerMission; ·import psn.razerpen.time.TimeStruct; 使用方法: //1·继承AccuracyTimerMission接口,创建一个类。 class MyTimer implements AccuracyTimerMission { //2·指定一个周期 int nDelay=1000; //3·重写run方法(如不需要使用新线程执行任务,也可留空) /** * 任务线程,本函数继承自Runnable */ @Override public void run() { System.out.println(new TimeStruct()); } //4·重写RunInCurrentThread(long nCurrentMilliSecond)方法。该方法接收当前时间,并返回下一次执行的时间。如果返回值不大于nCurrentMilliSecond则中止计时器。该方法必须重写。 /** * 接收当前时间的毫秒值,并返回下一次执行的毫秒值。如果返回的下一个时间早于当前时间,则退出 */ @Override public long RunInCurrentThread(long nCurrentMilliSecond) { return nCurrentMilliSecond+=nDelay; } } //5·创建主线程代码 public class TestTimer { public static void main(String[] args) throws InterruptedException { //6·创建一个AccuracyTimer对象,并指定一个任务。 AccuracyTimer at=new AccuracyTimer(new MyTimer()); //7·(可选)如果不需要在新线程中启动任务,则写 // at.SetNewThreadEnabled(false); //否则不写或者写 // at.SetNewThreadEnabled(true); //8·(可选)设定第一次启动的时间点SetNextMissionTime/SetNextMissionMilliSecond或延迟时间SetNextMissionMilliSecondFromNow //设置为当前这一分钟的第59秒后启动(不写此行则表示直接启动) at.SetNextMissionTime(Integer.MIN_VALUE, -1, -1, -1, -1, 59, 0); //9·启动定时器 at.Start(); //10·主线程继续 for(int i=0;i<60;++i){ Thread.sleep(1000); } //11·结束定时器 at.End(); } } 详见sample.razerpen.thread包中TestTimer及各代码文件中注释

文件下载

资源详情

[{"title":"( 12 个子文件 18KB ) JAVA精确定时器","children":[{"title":"src","children":[{"title":"sample","children":[{"title":"razerpen","children":[{"title":"thread","children":[{"title":"TestTimer.java <span style='color:#111;'> 1.01KB </span>","children":null,"spread":false}],"spread":true}],"spread":true}],"spread":true},{"title":"psn","children":[{"title":"razerpen","children":[{"title":"time","children":[{"title":"TimeStruct.java <span style='color:#111;'> 5.96KB </span>","children":null,"spread":false}],"spread":true},{"title":"thread","children":[{"title":"AccuracyTimer.java <span style='color:#111;'> 4.82KB </span>","children":null,"spread":false},{"title":"AccuracyTimerMission.java <span style='color:#111;'> 495B </span>","children":null,"spread":false}],"spread":true},{"title":"java","children":[{"title":"DebugLog.java <span style='color:#111;'> 2.22KB </span>","children":null,"spread":false}],"spread":true}],"spread":true}],"spread":true}],"spread":true},{"title":"bin","children":[{"title":"sample","children":[{"title":"razerpen","children":[{"title":"thread","children":[{"title":"MyTimer.class <span style='color:#111;'> 768B </span>","children":null,"spread":false},{"title":"TestTimer.class <span style='color:#111;'> 1.09KB </span>","children":null,"spread":false}],"spread":true}],"spread":true}],"spread":true},{"title":"psn","children":[{"title":"razerpen","children":[{"title":"time","children":[{"title":"TimeStruct.class <span style='color:#111;'> 4.10KB </span>","children":null,"spread":false}],"spread":true},{"title":"thread","children":[{"title":"AccuracyTimer$1.class <span style='color:#111;'> 1.87KB </span>","children":null,"spread":false},{"title":"AccuracyTimer.class <span style='color:#111;'> 4.40KB </span>","children":null,"spread":false},{"title":"AccuracyTimerMission.class <span style='color:#111;'> 223B </span>","children":null,"spread":false}],"spread":true},{"title":"java","children":[{"title":"DebugLog.class <span style='color:#111;'> 3.60KB </span>","children":null,"spread":false}],"spread":true}],"spread":true}],"spread":true}],"spread":true}],"spread":true}]

评论信息

免责申明

【只为小站】的资源来自网友分享,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,【只为小站】 无法对用户传输的作品、信息、内容的权属或合法性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论 【只为小站】 经营者是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。
本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二条之规定,若资源存在侵权或相关问题请联系本站客服人员,zhiweidada#qq.com,请把#换成@,本站将给予最大的支持与配合,做到及时反馈和处理。关于更多版权及免责申明参见 版权及免责申明