java定时操作之Timer和TimerTas(二)

2014-11-24 01:45:17 · 作者: · 浏览: 1
op extra reference to prevent memory leak

fixDown(1); 内容来自dedecms

}

/**

* Removes the ith element from queue without regard for maintaining the

* heap invariant. Recall that queue is one-based, so 1 <= i <= size.

*/

void quickRemove(int i) {

// 断言,在这里只起测试作用

assert i <= size;

queue[i] = queue[size];

queue[size--] = null; // Drop extra ref to prevent memory leak

}

/**

* Sets the nextExecutionTime associated with the head task to the specified

* value, and adjusts priority queue accordingly.

*/

void rescheduleMin(long newTime) {

queue[1].nextExecutionTime = newTime;

fixDown(1);

}

/**

* Returns true if the priority queue contains no elements.

*/

boolean isEmpty() {

return size == 0;

}

/**

* Removes all elements from the priority queue.

*/ copyright dedecms

void clear() {

// Null out task references to prevent memory leak

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

queue[i] = null;

size = 0;

}

// 进行队列中任务优先级调整. fixUp方法的作用是尽量将队列中指定位置(k)的任务向队列前面移动,

// 即提高它的优先级. 因为新加入的方法很有可能比已经在任务队列中的其它任务要更早执行.

private void fixUp(int k) {

while (k > 1) {

int j = k >> 1;// 左移一位,相当于除以2

if (queue[j].nextExecutionTime <= queue[k].nextExecutionTime)

break;

TimerTask tmp = queue[j];

queue[j] = queue[k];

queue[k] = tmp;

k = j;

}

}

// 从任务队列中移除一个任务的过程, 首先直接将当前任务队列中最后一个任务赋给queue[1],

// 然后将队列中任务数量--, 最后和上面类似, 但是这里是调用fixDown(int k)方法了, 尽量将k位置的任务向队列后面移动.

private void fixDown(int k) {

int j;

while ((j = k << 1) <= size && j > 0) {

if (j < size

&& queue[j].nextExecutionTime > queue[j + 1].nextExecutionTime)

j++; // j indexes smallest kid

if (queue[k].nextExecutionTime <= queue[j].nextExecutionTime)

break;

TimerTask tmp = queue[j];

queue[j] = queue[k];

queue[k] = tmp;

k = j;

}

}

/**

* Establishes the heap invariant (described above) in the entire tree,

* assuming nothing about the order of the elements prior to the call.

*/

void heapify() {

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

fixDown(i);

}

}

// 任务队列

class TaskQueue {

// 计时器任务数组,默认大小为128

private TimerTask[] queue = new TimerTask[128]; dedecms.com

private int size = 0;

int size() {

return size;

}

// 加入队列

void add(TimerTask task) {

// Grow backing store if necessary

if (size + 1 == queue.length)

// 队列以两倍的速度扩容

queue = Arrays.copyOf(queue, 2 * queue.length);

queue[++size] = task;

fixUp(size);

}

// 获取队列的地二个元素,即第一个任务,第一个元素存储的是

TimerTask getMin() {

return queue[1];

}

TimerTask get(int i) {

return queue[i];

}

// 消除头任务从优先队列。

void removeMin() {

queue[1] = queue[size];

queue[size--] = null; // Drop extra reference to prevent memory leak

fixDown(1);

}

/**

* Removes the ith element from queue without regard for maintaining the

* heap invariant. Recall that queue is one-based, so 1 <= i <= size.

*/

void quickRemove(int i) {

// 断言,在这里只起测试作用

assert i <= size;

queue[i] = queue[size];

queue[size--] = null; // Drop extra ref to prevent memory leak

}

/**

* Sets the nextExecutionTime associated with the head task to the specified

* value, and adjusts priority queue accordingly.

*/

void rescheduleMin(long newTime) {

queue[1].nextExecutionTime = newTime;

fixDown(1);

}

/**

* Returns true if the priority queue contains no elements.

*/

boolean isEmpty() {

return size == 0;

}

/**

* Removes all elements from the priority queue.

*/

void clear() {

// Null out task references to prevent memory leak

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

queue[i] = null;

size = 0;

}

// 进行队列中任务优先级调整. fixUp方法的