java timer時間排程除錯理解

2021-06-26 07:32:01 字數 2396 閱讀 3772

先寫乙個timer並通過sechedule計畫函式執行自己定義的timertask時間排程任務

private timer mtimer = null;

mtimer = new timer();

mtimer.schedule(new mytimertask(), 1000 ,time * 1000);

class mytimertask extends timertask

}

最先函式進入到這裡
/**

* schedule a task for repeated fixed-delay execution after a specific delay.

** @param task

* the task to schedule.

* @param delay

* amount of time in milliseconds before first execution.

* @param period

* amount of time in milliseconds between subsequent executions.

* @throws illegalargumentexception

* if or .

* @throws illegalstateexception

* if the has been canceled, or if the task has been

* scheduled or canceled.

*/public void schedule(timertask task, long delay, long period)

scheduleimpl(task, delay, period, false);

}/** schedule a task.

*/private void scheduleimpl(timertask task, long delay, long period, boolean fixed)

long when = delay + system.currenttimemillis();

if (when < 0)

synchronized (task.lock)

if (task.cancelled)

task.when = when;

task.period = period;

task.fixedrate = fixed;

}// insert the newtask into queue

impl.inserttask(task);

}}

修改task的屬性設定好第一次的執行會在當前時間delay毫秒後,每隔period毫秒這行一次。

為了保證所有的時間計畫任務的執行都不會發生衝突,使用synchronized限定同一時間只能有乙個執行緒呼叫

scheduleimpl

private void inserttask(timertask newtask)

接下來就要將自己定義的timertask插入到乙個陣列中

private int default_heap_size = 256;

private int size = 0;

private timertask timers = new timertask[default_heap_size];

預設設定用於儲存timertask時間計畫任務的堆的大小事256

public void insert(timertask task) 

timers[size++] = task;

upheap();

}

當timertask堆中沒有計畫任務,或者堆中的任務數量已經達到了其前一次設定的堆的大小則將堆的大小公升級為以前堆的大小的一倍

/**

* contains scheduled events, sorted according to

* field of taskscheduled object.

*/private timerheap tasks = new timerheap();

更新計畫任務堆,用根據計畫起始時間二分排序對新插入的計畫任務timertask出入到合適位置

private void upheap() 

}

反除錯 時間

include include include using namespace std void isdebuger tbefore tafter return intmain rdtsc時鐘檢測反除錯 使用時鐘檢測方法是利用rdtsc這個彙編指令,它返回至系統重新啟動以來的時鐘數,並且將其作為乙個...

程序排程 時間片輪轉

include include include typedef struct table node node creat void p2 next null return head 輸出函式 void print node head 對程序表按優先數從大到小排序 node insert node h...

程序排程和程序時間

由核心決定 排程策略,排程優先順序 nice值決定優先順序,nice越低優先順序越高 nice的範圍在 0 2 nzero 1 nzero是系統預設的nice值 只有特權程序允許提高排程許可權 關於nzero 定義nzero的標頭檔案因系統而異,除了標頭檔案,linux3.2 可以通過非標準的sys...