首先看段简单的线程代码:
public class Demo {
public static void main(String[] args) {
Thread t=new Thread(new StopRunnable());
t.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("xxxxxxxxxxx");
e.printStackTrace();
}
Thread.currentThread().interrupt();
System.out.println(Thread.currentThread().isInterrupted());
t.interrupt();
System.out.println("main thread over.");
}
}class StopRunnable implements Runnable{
@Override
public void run() {
int i=1;
while(!Thread.currentThread().interrupted()){
System.out.println(i++);
}
}
}问题: 下面这个程序片段我不是很明白Thread.currentThread().interrupt();
System.out.println(Thread.currentThread().isInterrupted());
t.interrupt();
System.out.println("main thread over.");
线程t在主线程中运行,Thread.currentThread().interrupt(); 使主线程终止。
主线程终止代表程序已经结束。后面的代码应该不执行。
但实际控制台却有输出,为什么啊??????
public class Demo {
public static void main(String[] args) {
Thread t=new Thread(new StopRunnable());
t.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("xxxxxxxxxxx");
e.printStackTrace();
}
Thread.currentThread().interrupt();
System.out.println(Thread.currentThread().isInterrupted());
t.interrupt();
System.out.println("main thread over.");
}
}class StopRunnable implements Runnable{
@Override
public void run() {
int i=1;
while(!Thread.currentThread().interrupted()){
System.out.println(i++);
}
}
}问题: 下面这个程序片段我不是很明白Thread.currentThread().interrupt();
System.out.println(Thread.currentThread().isInterrupted());
t.interrupt();
System.out.println("main thread over.");
线程t在主线程中运行,Thread.currentThread().interrupt(); 使主线程终止。
主线程终止代表程序已经结束。后面的代码应该不执行。
但实际控制台却有输出,为什么啊??????
谢谢你的建议,ibm developerworks的文章是要好好拜读一下。 在看精辟的文章之前,我想先把基础知识掌握好一点。