package com.skex.timer;
import java.util.Timer;
import java.util.TimerTask;
public class TryTimer {
public static void main(String[] args) {
TimerTask timeTask_speed = new TimerTask() {
int counter=0;
@Override
public void run() {
System.out.println("count=" + counter++);
if(counter>10){
boolean bC=this.cancel();
System.out.println("cancel: " + (bC?"OK":"NG"));
}
}
};
Timer timer_speed = new Timer();
timer_speed.schedule(timeTask_speed, 0, 1000);
/*
count=0
main end!
count=1
count=2
count=3
count=4
count=5
count=6
count=7
count=8
count=9
count=10
cancel: OK
[进程 still running ,not terminated!]
*/
Thread thread=new Thread(new Runnable() {
int counter=0;
@Override
public void run() {
// TODO Auto-generated method stub
try {
System.out.println("count=" + counter++);
if(counter>10){
System.out.println("thread end. ");
}
Thread.sleep(1000);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
});
//thread.start();
/*
main end!
count=0
[进程 terminated!]
*/
System.out.println("main end!");
}
}