public class InterruptThread2 extends Thread{
public static void main(String[] args) {
try {
InterruptThread2 t = new InterruptThread2();
t.start();
Thread.sleep(1000);
t.interrupt();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void run() {
super.run();
for(int i = 0; i <= 800000; i++) {//设置中断标识,线程没有影响,还会继续运行。线程只有在方法执行完了,才会退出,
System.out.println("i=" + i);
}
}
public void run1() {
super.run();
for(int i = 0; i <= 200000; i++) {
//判断是否被中断
if(Thread.currentThread().isInterrupted()){
//设置中断标识,线程没有影响,还会继续运行。这里要线程的方法执行完,线程就退出了。
//处理中断逻辑
for(int j = 0;j < 10 ;j++) {
System.out.println("sssss" + j);
}
break;
}
System.out.println("i=" + i);
}
}
}
public class InterruptThread1 extends Thread{
public static void main(String[] args) {
try {
InterruptThread1 t = new InterruptThread1();
t.start();
Thread.sleep(1000);
t.stop();//停止不了线程
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void run() {
super.run();
for(int i = 0; i <= 800000; i++) {//设置中断标识,线程没有影响,还会继续运行。线程只有在方法执行完了,才会退出,
try {
Thread.currentThread().sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("i=" + i);
}
}
}