package testJavaAll;
import java.util.Timer;
import java.util.TimerTask;
/**
* 测试线程开启和停止cancel的
*
* 2014年10月21日 10:34:57
*
* yjbo
*/
public class testThread {
Timer timer;
private int x = 0;
public testThread(int seconds, int seconds2) {
timer = new Timer();
timer.schedule(new RemindTask(), seconds * 1000, seconds2 * 1000);
}
class RemindTask extends TimerTask {
public void run() {
x++;
System.out.println("----xx---" + x);
if (x < 10) {
// 开启线程
System.out.println("Time's up!");
} else if (x == 10) {
// 停止线程
timer.cancel(); // Terminate the timer thread
System.out.println("Time's up!`````000");
}
}
}
public static void main(String args[]) {
System.out.println("About to schedule task.");
new testThread(1, 2);
System.out.println("Task scheduled.");
}
}
// }
import java.util.Timer;
import java.util.TimerTask;
/**
* 测试线程开启和停止cancel的
*
* 2014年10月21日 10:34:57
*
* yjbo
*/
public class testThread {
Timer timer;
private int x = 0;
public testThread(int seconds, int seconds2) {
timer = new Timer();
timer.schedule(new RemindTask(), seconds * 1000, seconds2 * 1000);
}
class RemindTask extends TimerTask {
public void run() {
x++;
System.out.println("----xx---" + x);
if (x < 10) {
// 开启线程
System.out.println("Time's up!");
} else if (x == 10) {
// 停止线程
timer.cancel(); // Terminate the timer thread
System.out.println("Time's up!`````000");
}
}
}
public static void main(String args[]) {
System.out.println("About to schedule task.");
new testThread(1, 2);
System.out.println("Task scheduled.");
}
}
// }