public static void main(String[] args){
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println(new Date() + " : It is time to send mail to leader ... ");
}
}, 2000);
}
public static void main(String[] args){
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println(new Date() + " : It is time to send mail to leader ... ");
}
}, 2000, 1000);
}
public static void main(String[] args) {
System.out.println(new Date() +" : 程序开始执行 ...");
Timer timer = new Timer();
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.SECOND, 30);
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println(new Date() + " : It is time to send mail to leader ... ");
}
}, calendar.getTime());
}
public static void main(String[] args) {
System.out.println(new Date() +" : 程序开始执行 ...");
Timer timer = new Timer();
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.SECOND, 10);
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println(new Date() + " : It is time to send mail to leader ... ");
}
}, calendar.getTime(),3000);
}
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
</dependencies>
public class MyTask1 {
public void sendMail(){
System.out.println(new Date() + " : It is time to send mail to leader ...");
}
}
<bean id="task1" class="timerdemo.MyTask1"></bean>
<task:scheduled-tasks>
<task:scheduled ref="task1" method="sendMail" initial-delay="2000" fixed-delay="5000"/>
</task:scheduled-tasks>
<task:scheduled-tasks>
<task:scheduled ref="task1" method="sendMail" initial-delay="2000" fixed-rate="5000"/>
</task:scheduled-tasks>
<task:scheduled-tasks>
<task:scheduled ref="task1" method="sendMail" cron="0 0 9,18 * * *"/>
</task:scheduled-tasks>
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |