Java Timer????????á???????????У?
???????????? ???????[ 2015/8/5 13:42:11 ] ????????????????
??????????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);
???????????????????????漰???????????????????SPASVOС??(021-61079698-8054)?????????????????????????
??????
Java???????????Щ???????????????Java????????????????Java?б???Map????????Java Web???????????????Java??????????????д?????Java????????7???????????????????????(java .net ?????)???Java??????????Python??????Java webdriver??λ????????′????е?????Java??д??????????????????Java???????????????JavaScript????????????Java?????????????????? Java???????10??????????????Java?м????????????????java???????ü???????????м???????????????????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11????????
?????????App Bug???????????????????????Jmeter?????????QC??????APP????????????????app?????е????????jenkins+testng+ant+webdriver??????????????JMeter????HTTP???????Selenium 2.0 WebDriver ??????