package com.susan.thread;
import java.util.Timer;
import java.util.TimerTask;
/**
* 二、传统定时器的回顾
*
* @author dahai
*
*/
public class ThraditionalTimerTest {
public staticvoid main(String[] args) {
// TODO Auto-generated method stub
// 启动后两秒执行一下就没有了
new Timer().schedule(new TimerTask() {
@Override
publicvoid run() {
//TODO Auto-generated method stub
System.out.println("来了,来了哦");
}
}, 2000);
/**
* 启动后10秒就启动,每隔2秒启动一次
*/
new Timer().schedule(new TimerTask() {
@Override
publicvoid run() {
//TODO Auto-generated method stub
System.out.println("我来了!!!!!");
}
}, 10000, 2000);
/**
* 走两次就没了
*/
new Timer().schedule(new TimerTask() {
@Override
publicvoid run() {
//TODO Auto-generated method stub
System.out.println("我来了!!!!!");
new Timer().schedule(new TimerTask() {
@Override
publicvoid run() {
//TODO Auto-generated method stub
System.out.println("他来了!!!!!");
}
}, 2000);
}
}, 2000);
/**
* 启动后2秒就启动,每隔2秒启动一次
*/
class MyTimerTaskextends TimerTask {
@Override
publicvoid run() {
//TODO Auto-generated method stub
System.out.println("他来了!!!!!");
new Timer().schedule(/**
* new TimerTask() {
*
*@Override public void run() { // TODO
* Auto-generated method stub
* System.out.println(
* "他来了!!!!!"); }
*
* },
*/
new MyTimerTask(), 2000);
}
}
new Timer().schedule(new TimerTask() {
@Override
publicvoid run() {
//TODO Auto-generated method stub
System.out.println("我来了!!!!!");
new Timer().schedule(/**
* new TimerTask() {
*
*@Override public void run() { // TODO
* Auto-generated method stub
* System.out.println(
* "他来了!!!!!"); }
*
* }
*/
new MyTimerTask(), 2000);
}
}, 2000);
}
}