??????????TimerThread????????
??????????????????Timer???????????????????????????????????????TimerThread?????default???????????????????ò?????????Timer??????????
??????????????????Щ????
????????????????thread???????????????????????
????private TaskQueue queue = new TaskQueue();
?????????????????????У??????????????2???????????????????????????????????????????????????????????
???????滹????????????threadReaper?? ????Object??????????д??finalize????????????????????????????????????????????GC??????????timer?????????? ????????????δ??cancel???????????е???????????????????????????????????????????????javaд???????????????????
?????????????????????????
????????????6?????????????????????о?????????????????
?????????·?????
????public void schedule(TimerTask task?? long delay)
????????????£?
????1 public void schedule(TimerTask task?? long delay) {
????2        if (delay < 0)
????3            throw new IllegalArgumentException("Negative delay.");
????4        sched(task?? System.currentTimeMillis()+delay?? 0);
????5    }
??????????????????????????task?????????????????System.currentTimeMillis()+delay??????????????е????? ??????????????Date???????.getTime()????????????Date??????????????????????????????????????0???????????????? ??????????????????????????????????????????????sched?????????????????????????????????????????????
???????????????
????public void schedule(TimerTask task?? long delay??long period)
??????????
????public void schedule(TimerTask task?? long delay?? long period) {
????if (delay < 0)
????throw new IllegalArgumentException("Negative delay.");
????if (period <= 0)
????throw new IllegalArgumentException("Non-positive period.");
????sched(task?? System.currentTimeMillis()+delay?? -period);
????}
??????????????????sched????????????????????????????????????????????period????????????????0??????????????????????? ????????????????????period??????????????????????????????????1000???????sched????????-1000???????????? ????????????????????????????????????????????????????壬???????????????????Щ?????????
??????????????????
????public void scheduleAtFixedRate(TimerTasktask??long delay??long period)
??????????
????public void scheduleAtFixedRate(TimerTask task?? long delay?? long period) {
????if (delay < 0)
????throw new IllegalArgumentException("Negative delay.");
????if (period <= 0)
????throw new IllegalArgumentException("Non-positive period.");
????sched(task?? System.currentTimeMillis()+delay?? period);
????}
??????????????period????????????????????????????????????????????壬?????????????????????? scheduleAtFixedRate????scheduleAtFixedRate??schedule??????????????£??????????????Χ????? ???????????????????????????????????????schedule????????????scheduleAtFixedRate???????????? scheduleAtFixedRate???????????????????schedule????????????????Щ??????????壬???????????
????????sched??????????壺
private void sched(TimerTask task?? long time?? long period) {
if (time < 0)
throw new IllegalArgumentException("Illegal execution time.");
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();
}
}
????queue???????У??????????????????????????????????????????????????????????timer???????????????????task???????????????????nextExecutionTime???????????????period??????????state?????????????????queue?????У??????notify????????????notify??????????????????????????
??????????????????task???????queue?????????????????queue?????Щ????????????????????queue??????TaskQueue??
????class TaskQueue {
????private TimerTask[] queue = new TimerTask[128];
????private int size = 0;
?????????TaskQueue???????????????飬?????size???е???ArrayList??????????128???????? ???ArrayList?????????????????????????濽??????????????Timer?????????????task??????????128????????????????? ????add(TimerTask)??size()??getMin()??get(int)??removeMin()??quickRemove(int)?? rescheduleMin(long newTime)??isEmpty()??clear()??fixUp()??fixDown()??heapify()??
????????????
????1????????TimerTask???????
??????????дrun????.
public class MyTask extends TimerTask
{
@Override
public void run()
{
SimpleDateFormat sdf = null;
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
System.out.println("??????" + sdf.format(new Date()));
}
}
public class TestTask
{
public static void main(String[] args)
{
Timer t = new Timer(); // ????Timer????
MyTask task = new MyTask(); //????????
t.schedule(task?? 1000??2000);//???????????У?1???????2????????
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MINUTE?? 30);
t.schedule(task?? cal.getTime() ?? 2000);
}
}
????2?????????????????
????Timer timer = new Timer();
????timer.scheduleAtFixedRate(new TimerTask() {
????public void run() {
????System.out.println("abc");
????}
????}?? 1000 ?? 1000);