使用 java(1.5版本之后)线程池 ScheduledExecutorService 可以很方便的实现定时任务;
package bdqn;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* 定时任务测试
* @author WWP
* @date 2017-2-12 下午7:53:23
*/
public class Test {
// 线程调度池
private static ScheduledExecutorService executor = Executors.newScheduledThreadPool(5);
public static void main(String[] args) {
Thread th1 = new Thread(new SelfTask());
System.out.println("启动程序" + new Date());
/* 如果delay 时间小于0, 则立即发送 */
executor.schedule(th1, -1000, TimeUnit.MILLISECONDS);
}
}
/**
* 测试线程类
* @author WWP
* @date 2017-2-12 下午10:54:22
*/
class SelfTask implements Runnable{
@Override
public void run() {
System.out.println("--执行线程--" + new Date());
}
}