package liu.dh.thread1;
import java.sql.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Timer;
import java.util.TimerTask;
public class Demo3_Timer {
/**
* @param args
* public class Timer extends Object一种工具,线程用其安排以后在后台线程中执行的任务。
可安排任务执行一次,或者定期重复执行。
void schedule(TimerTask task, Date time)
安排在指定的时间执行指定的任务。
void schedule(TimerTask task, Date firstTime, long period)
安排指定的任务在指定的时间开始进行重复的固定延迟执行。
*/
public static void main(String[] args) {
Timer timer = new Timer();
Date date = new Date(System.currentTimeMillis());
timer.schedule(new Ttm(),date,1);
System.out.println(DateFormat.getTimeInstance().format(date));
}
}
class Ttm extends TimerTask{
@Override
public void run() {
// TODO Auto-generated method stub
Ttm.getTime();
}
public static void getTime() {
Date date = new Date(System.nanoTime());
System.out.println(DateFormat.getTimeInstance().format(date));
}
}
|
|