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

南昌 网站百度seo软件

南昌 网站,百度seo软件,安徽网站建,杭州企业如何建网站Java中的Timer类是一个定时调度器,用于在指定的时间点执行任务。JDK 中Timer类的定义如下:public class Timer {/*** The timer task queue. This data structure is shared with the timer* thread. The timer produces tasks, via its various sched…

Java中的Timer类是一个定时调度器,用于在指定的时间点执行任务。JDK 中Timer类的定义如下:

public class Timer {/*** The timer task queue.  This data structure is shared with the timer* thread.  The timer produces tasks, via its various schedule calls,* and the timer thread consumes, executing timer tasks as appropriate,* and removing them from the queue when they're obsolete.*/private final TaskQueue queue = new TaskQueue();/*** The timer thread.*/private final TimerThread thread = new TimerThread(queue);
}

以上就是Timer中最重要的两个成员变量:

1. TaskQueue:一个任务队列,用于存储已计划的定时任务。任务队列按照任务的执行时间进行排序,确保最早执行的任务排在队列前面。在队列中的任务可能是一次性的,也可能是周期性的。2. TimerThread:Timer 内部的后台线程,它负责扫描 TaskQueue 中的任务,检查任务的执行时间,然后在执行时间到达时执行任务的 run() 方法。

任务的定时调度的核心代码就在TimerThread中:

class TimerThread extends Thread {boolean newTasksMayBeScheduled = true;/*** 存储 TimerTask 的队列*/private TaskQueue queue;TimerThread(TaskQueue queue) {this.queue = queue;}public void run() {try {mainLoop();} finally {synchronized (queue) {newTasksMayBeScheduled = false;queue.clear(); }}}/*** 主要的计时器循环。 */private void mainLoop() {while (true) {try {TimerTask task;boolean taskFired;synchronized (queue) {// 等待队列变为非空while (queue.isEmpty() && newTasksMayBeScheduled)queue.wait();if (queue.isEmpty())break; // 队列为空,将永远保持为空;线程终止// 队列非空;查看第一个事件并执行相应操作long currentTime, executionTime;task = queue.getMin();synchronized (task.lock) {if (task.state == TimerTask.CANCELLED) {queue.removeMin();continue;  // 无需执行任何操作,再次轮询队列}currentTime = System.currentTimeMillis();executionTime = task.nextExecutionTime;if (taskFired = (executionTime <= currentTime)) {if (task.period == 0) { // 非重复,移除queue.removeMin();task.state = TimerTask.EXECUTED;} else { // 重复任务,重新安排queue.rescheduleMin(task.period < 0 ? currentTime   - task.period: executionTime + task.period);}}}if (!taskFired) // 任务尚未触发;等待queue.wait(executionTime - currentTime);}if (taskFired)  // 任务触发;运行它,不持有锁task.run();} catch (InterruptedException e) {}}}
}

可以看到,TimerThread的实际是在运行mainLoop方法,这个方法一进来就是一个while(true)的循环,他在循环中不断地从TaskQueue中取出第一个任务,然后判断他是否到达执行时间了,如果到了,就触发任务执行。否则就继续等一会再次执行。不断地重复这个动作,从队列中取出第一个任务进行判断,执行。。。

这样只要有新的任务加入队列,就在队列中按照时间排序,然后唤醒timerThread重新检查队列进行执行就可以了。代码如下:

private void sched(TimerTask task, long time, long period) {if (time < 0)throw new IllegalArgumentException("Illegal execution time.");// Constrain value of period sufficiently to prevent numeric// overflow while still being effectively infinitely large.if (Math.abs(period) > (Long.MAX_VALUE >> 1))period >>= 1;synchronized(queue) {if (!thread.newTasksMayBeScheduled)throw new IllegalStateException("Timer already cancelled.");synchronized(task.lock) {if (task.state != TimerTask.VIRGIN)throw new IllegalStateException("Task already scheduled or cancelled");task.nextExecutionTime = time;task.period = period;task.state = TimerTask.SCHEDULED;}//新任务入队列queue.add(task);//唤醒任务if (queue.getMin() == task)queue.notify();}}

http://www.dtcms.com/a/459285.html

相关文章:

  • 为什么网站经常被攻击手机软件商城下载app
  • 游戏ui设计网站wordpress 迅搜
  • 株洲网站平台搭建地方房产网站APP如何做
  • 宜春网站开发公司电话团员团干部如何登录到系统
  • 3d 网站设计制作一个工厂小程序收费
  • 济南智能网站建设电话东莞做网站推广
  • 做网站怎样做关键词挖掘啊爱站网
  • 网站推广的方法和渠道工程建设标准强制性条文最新版本
  • 上海建设摩托车官方网站怎么做微信推广和宣传
  • 网站中文字内容左右切换的js代码网站建设如何获取客户
  • 品牌网站建设设计公司网址二维码生成器
  • 不能访问子目录的网站动漫做暧视频网站
  • 十八个免费的舆情网站做网站用什么空间
  • wordpress做得比较大的网站金蝶财务软件一般多少钱
  • 安徽网站建设价格网站如何进行seo
  • 做毕设的网站万禁止wordpress获取隐私
  • 网页设计与制作实训报告2000字优化seo是什么
  • 做境外旅游的网站铜川市建设集团网站
  • 潍坊制作网站网络推广怎么找客户资源
  • 做拼多多网站免费课程怎样修改网站模板
  • html5网站开发工具有哪些wordpress mo po
  • windows服务器怎么建设网站flash网站首页
  • 电子商务网站开发综合实训报告有用模板网在线制作免费网站
  • 西宁网站建设公司怎么制作游戏app软件
  • 如何做一个:心想事成辅助小程序(先实现简单的多agent协同协作)
  • 餐饮网站开发洛阳建设信息网站
  • 网站建设智推网seo推广公司排名
  • 深圳网站建设哪个在线看视频网站怎么做
  • 网站建设的用途是什么网加商学院的wordpress
  • 酒店网站的规划与建设关键词seo排名优化软件