62,616
社区成员
发帖
与我相关
我的任务
分享
package com.huaxia;
public class Demo {
public static void main(String[] args) {
Thread t=new Thread(new StopRunnable());
System.out.println("子线程是否存活1: "+t.isAlive());
t.start();
System.out.println("子线程是否存活2: "+t.isAlive());
try {
Thread.sleep(10);
} catch (InterruptedException e) {
System.out.println("xxxxxxxxxxx");
e.printStackTrace();
}
System.out.println("子线程是否存活3: "+t.isAlive());
Thread.currentThread().interrupt();
t.interrupt();
System.out.println("子线程是否存活4: "+t.isAlive());
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++);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}