项目中往往会遇到需要定时的任务,例如订单,当用户在某个规定时间内没有操作订单时,订单状态将会发生改变。
那么在这种情况下,我们会用到定时器。
举例:
import java.util.Timer; /**
* java定时器
* @author Administrator
*
*/
public class TimeTaskTest {
public static void main(String[] args) {
Timer timer = new Timer();
System.out.println("开始执行");
timer.schedule(new Test(), 10 * 1000);
System.out.println("呵呵呵");
} }
实例化 Timer 并调用 schedule 方法
schedule有四个重载方法:
以第二个举例子
timer.schedule(new Test(), 10 * 1000);
第一个参数是要执行的任务,第二个参数是毫秒数的时间,比如 10秒 就是10*1000;
【待续】