?? timer.java
字號:
package practises.thread;
import java.util.Date;
import java.util.TreeSet;
import java.util.Comparator;
public class timer
{
//已經(jīng)排序的集合保存timer負責的任務
//使用一個比較函數(shù)按時間執(zhí)行任務
TreeSet tasks = new TreeSet(new TimerTaskComparator());
//用來執(zhí)行任務的線程
TimerThread timer1;
public timer() { this(false); }
public timer(boolean isDaemon)
{
timer1 = new TimerThread(isDaemon);
timer1.start();
}
//停止線程,放棄任務
public void cancel()
{
synchronized(tasks)
{
timer1.pleaseStop();
tasks.clear();
tasks.notify();
}
}
public void schedule(timertask task, long delay)
{
task.schedule(System.currentTimeMillis() + delay, 0, false);
schedule(task);
}
public void schedule(timertask task, Date time)
{
task.schedule(time.getTime(), 0, false);
schedule(task);
}
public void schedule(timertask task, Date firstTime, long period)
{
task.schedule(firstTime.getTime(), period, false);
schedule(task);
}
public void schedule(timertask task, long delay, long period)
{
task.schedule(System.currentTimeMillis() + delay, period, false);
schedule(task);
}
public void scheduleAtFixedRate(timertask task, long delay, long period)
{
task.schedule(System.currentTimeMillis() + delay, period, true);
schedule(task);
}
public void scheduleAtFixedRate(timertask task, Date firstTime,long period)
{
task.schedule(firstTime.getTime(), period, true);
schedule(task);
}
void schedule(timertask task)
{
synchronized(tasks)
{
tasks.add(task);
tasks.notify();
}
}
static class TimerTaskComparator implements Comparator
{
public int compare(Object a, Object b)
{
timertask t1 = (timertask) a;
timertask t2 = (timertask) b;
long diff = t1.nextTime - t2.nextTime;
if (diff < 0) return -1;
else if (diff > 0) return 1;
else return 0;
}
public boolean equals(Object o) { return this == o; }
}
class TimerThread extends Thread
{
//此標志為真,表示線程停止。注意它被申明為volatile,這樣可被另外一個
//線程異步改變,所以線程必須總是讀入真值,而且不用緩存版本。
volatile boolean stopped = false;
public TimerThread(boolean isDaemon) { setDaemon(isDaemon); }
public void pleaseStop() { stopped = true; }
public void run()
{
timertask readyToRun = null;
while(!stopped)
{
if (readyToRun != null)
{
if (readyToRun.cancelled)
{
readyToRun = null;
continue;
}
readyToRun.run();
if (readyToRun.reschedule()) schedule(readyToRun);
readyToRun = null;
continue;
}
synchronized(tasks)
{
long timeout;
if(tasks.isEmpty())
{
timeout=0;
}
else
{
timertask t=(timertask) tasks.first();
timeout=t.nextTime-System.currentTimeMillis();
if(timeout<=0)
{
readyToRun=t;
tasks.remove(t);
continue;
}
}
try
{
tasks.wait(timeout);
}
catch(InterruptedException e){}
}
}
}
}
public static class test
{
public static void main(String[] args)
{
final timertask t1=new timertask()
{
public void run()
{System.out.println("boom");}
};
final timertask t2=new timertask()
{
public void run()
{
System.out.println("\tBOOM");
}
};
final timertask t3=new timertask()
{
public void run()
{
t1.cancel();
t2.cancel();
}
};
final timer timer1=new timer();
timer1.schedule(t1, 0, 500);
timer1.schedule(t2, 2000, 2000);
timer1.schedule(t3, 5000);
timer1.scheduleAtFixedRate(new timertask()
{
public int times = 5;
public void run()
{
System.out.println(times--);
if (times == 0) timer1.cancel();
}
},5000,500);
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -