第一种:
package com.timertask.test;
/**
* 三种方法实现java定时义务
* @author li
*
*/
public class TimerTask {
static int i = 1;
public static void main(String[] args) {
/**
* 普通thread
* 这是最多见的,创立一个thread,然后让它在while重复里一直运行着,
* 通过sleep办法来到达定时义务的后果。这样可以迅速简略的实现,代码以下:
* @author li
*
*/
final long timeInterval = 3000;
Runnable runnable = new Runnable(){
@Override
public void run() {
while(true){
System.out.println(i+"、Hello LingGe!");
i++;
try {
Thread.sleep(timeInterval);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
Thread thread = new Thread(runnable);
thread.start();
}
}package com.timertask.test;
/**
* 三种方