对于开发游戏项目的同胞来说,Timer 这个东西肯定不会陌生,今天对以前自己经常使用的定时进行了一番小小的总结!没有写具体实现的原理,只是列举出了其中的四种比较常见的使用方法,相对而言,所以只要按照其所列举的例子仿照即可!
import
java.util.Calendar;
import
java.util.Date;
import
java.util.Timer;
import
java.util.TimerTask;
public
class
TimeTest {
public
static
void
main(String[] args) {
timer1();
}
public
static
void
timer1() {
Timer timer =
new
Timer();
timer.schedule(
new
TimerTask() {
public
void
run() {
System.out.println(
"-------设定要指定任务--------"
);
}
},
2000
);
}
public
static
void
timer2() {
Timer timer =
new
Timer();
timer.schedule(
new
TimerTask() {
public
void
run() {
System.out.println(
"-------设定要指定任务--------"
);
}
},
1000
,
5000
);
}
public
static
void
timer3() {
Timer timer =
new
Timer();
timer.scheduleAtFixedRate(
new
TimerTask() {
public
void
run() {
System.out.println(
"-------设定要指定任务--------"
);
}
},
1000
,
2000
);
}
public
static
void
timer4() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,
12
);
calendar.set(Calendar.MINUTE,
0
);
calendar.set(Calendar.SECOND,
0
);
Date time = calendar.getTime();
Timer timer =
new
Timer();
timer.scheduleAtFixedRate(
new
TimerTask() {
public
void
run() {
System.out.println(
"-------设定要指定任务--------"
);
}
}, time,
1000
*
60
*
60
*
24
);
}
}