JAVA_Thread_interrupt

package com.kk.thread;
/**
* 不调用stop方法退出线程
*
*/
public class TestThread {
public static void main(String[] args) {
Thread1 thread=new Thread1();
thread.start();
int index=0;
while(true){
if(index++==500){
thread.stopThread();
thread.interrupt();
break;
}
System.out.println(Thread.currentThread().getName());
}
System.out.println("main() exit");
}
}

class Thread1 extends Thread {
boolean b=false;
public synchronized void run() {
while(!b){
try {
wait();
} catch (InterruptedException e) {
if(b)return;
}
System.out.println("hallo");
}
}

public void stopThread(){
b=true;
}
}
原文地址:https://www.cnblogs.com/BigIdiot/p/2290447.html