java定时器的原理_JAVA Timer 定时器原理 | 学步园

本文详细介绍了Java中的Timer和TimerTask类的内部实现,包括TaskQueue的数据结构和TimerThread的工作原理。同时,提到了可能存在的TimerTask执行延迟问题,并通过一个示例展示了如何创建和调度TimerTask。实验表明,由于多线程竞争和任务调度的影响,TimerTask可能存在执行延迟的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、Timer 组成:TaskQueue  TimerThread

TaskQueue  是用来存储 TimerTask  ,TimerTask 是Runnable 子类

class TaskQueue

{

private TimerTask[] queue = new TimerTask[128];

private int size = 0;

int size()

{

return this.size;

}

void add(TimerTask paramTimerTask)

{

if (this.size + 1 == this.queue.length)

this.queue = ((TimerTask[])Arrays.copyOf(this.queue, 2 * this.queue.length));

this.queue[(++this.size)] = paramTimerTask;

fixUp(this.size);

}

TimerTask getMin()

{

return this.queue[1];

}

TimerTask get(int paramInt)

{

return this.queue[paramInt];

}

void removeMin()

{

this.queue[1] = this.queue[this.size];

this.queue[(this.size--)] = null;

fixDown(1);

}

void quickRemove(int paramInt)

{

assert (paramInt <= this.size);

this.queue[paramInt] = this.queue[this.size];

this.queue[(this.size--)] = null;

}

void rescheduleMin(long paramLong)

{

this.queue[1].nextExecutionTime = paramLong;

fixDown(1);

}

boolean isEmpty()

{

return (this.size == 0);

}

void clear()

{

for (int i = 1; i <= this.size; ++i)

this.queue[i] = null;

this.size = 0;

}

private void fixUp(int paramInt)

{

while (paramInt > 1)

{

int i = paramInt >> 1;

if (this.queue[i].nextExecutionTime <= this.queue[paramInt].nextExecutionTime)

return;

TimerTask localTimerTask = this.queue[i];

this.queue[i] = this.queue[paramInt];

this.queue[paramInt] = localTimerTask;

paramInt = i;

}

}

private void fixDown(int paramInt)

{

while (((i = paramInt << 1) <= this.size) && (i > 0))

{

int i;

if (((i < this.size) && (this.queue[i].nextExecutionTime > this.queue[(i + 1)].nextExecutionTime)) || (this.queue[paramInt].nextExecutionTime <= this.queue[(++i)].nextExecutionTime))

return;

TimerTask localTimerTask = this.queue[i];

this.queue[i] = this.queue[paramInt];

this.queue[paramInt] = localTimerTask;

paramInt = i;

}

}

void heapify()

{

for (int i = this.size / 2; i >= 1; --i)

fixDown(i);

}

}

TimerThread 只是一个线程,用来执TaskQueue里面的TimerTask  里面的run方法而不是start方法.

package java.util;

class TimerThread extends Thread

{

boolean newTasksMayBeScheduled = true;

private TaskQueue queue;

TimerThread(TaskQueue paramTaskQueue)

{

this.queue = paramTaskQueue;

}

public void run()

{

try

{

mainLoop();

}

finally

{

synchronized (this.queue)

{

this.newTasksMayBeScheduled = false;

this.queue.clear();

}

}

}

private void mainLoop()

{

while (true)

try

{

TimerTask localTimerTask;

int i;

synchronized (this.queue)

{

while ((this.queue.isEmpty()) && (this.newTasksMayBeScheduled))

this.queue.wait();

if (this.queue.isEmpty())

return;

localTimerTask = this.queue.getMin();

long l1;

long l2;

synchronized (localTimerTask.lock)

{

if (localTimerTask.state == 3)

{

this.queue.removeMin();

monitorexit;

}

l1 = System.currentTimeMillis();

l2 = localTimerTask.nextExecutionTime;

if ((i = (l2 <= l1) ? 1 : 0) != 0)

if (localTimerTask.period == 0L)

{

this.queue.removeMin();

localTimerTask.state = 2;

}

else

{

this.queue.rescheduleMin((localTimerTask.period < 0L) ? l1 - localTimerTask.period : l2 + localTimerTask.period);

}

}

if (i == 0)

this.queue.wait(l2 - l1);

}

if (i != 0)

localTimerTask.run();

}

catch (InterruptedException localInterruptedException)

{

}

}

}

2、里面的

3、可能存在的引起的问题就是TimerTask 执行会延迟。

4、TimerTask使用例子及实验测试说明(延迟性)

import java.util.Random;

import java.util.Timer;

import java.util.TimerTask;

public class TestTimer {

/**

* @param args

*/

public static void main(String[] args) {

Timer t = new Timer();

for(int i=1 ;i<101; i++){

MyTimerTask  mtt = new MyTimerTask("线程"+i+": ");

t.schedule(mtt, 2000);

}

}

}

class MyTimerTask extends TimerTask {

String s;

public MyTimerTask(String s) {

this.s = s;

}

public void run() {

try {

Random random = new Random();

for (int i = 0; i < 10; i++) {

int longTime = random.nextInt(10000);

Thread.sleep(longTime);

System.out.println(s + i + "  waitTime=" +longTime);

}

} catch (Exception e) {

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值