码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2014-10-21 12:22:37      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:定时线程   java   android   thread   开始与结束   

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.");
}
}
// }

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

标签:定时线程   java   android   thread   开始与结束   

原文地址:http://blog.csdn.net/yangjianbo456/article/details/40341693

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!