java_线程的开启与结束(可用于android)

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.");
}
}
// }
posted @ 2017-05-11 20:03  jzdwajue  阅读(128)  评论(0编辑  收藏  举报