日期:2014-05-20 浏览次数:20895 次
package test;
public class TestThread {
/**
* @param args
*/
public static void main(String[] args) {
test a = new test();
p(a);
p(a);
a.start();
p(a);
p(a);
p(a);
// a.stop(); <=== 这里是尝试用各种方法终端线程的地方
// a.interrupt();
p(a);
p(a);
p(a);
p(a);
p(a);
p(a);
p(a);
p(a);
p(a);
p(a);
p(a);
p(a);
p(a);
}
static long ts = System.currentTimeMillis();
private static void p(test a) {
System.out.println(System.currentTimeMillis() - ts
+ ": Thread.isAlive=" + a.isAlive());
try {
Thread.sleep(300);
} catch (InterruptedException e) {
}
}
static class test extends Thread {
public void run() {
System.out.println("I'm running");
sleepAnyWay(1000);
System.out.println("I'm running again");
sleepAnyWay(1000);
System.out.println("I'm gonna to quit");
sleepAnyWay(1000);
}
// 这个函数模拟线程中的用户函数把 interrupt异常吃掉的情况
void sleepAnyWay(long ts) {
try {
Thread.sleep(ts);
} catch (InterruptedException e) {
}
}
}
}