stop方法是过时,不被推荐的。因为它直接杀死了进程,比如线程进在写,或是在打开一个资源,后果严重!除非不得已,比如线程无响应。、
run方法结束,就代表了线程被结束!!
interruptedException是一个抛异常模式,虽然它也不太好。但有一定特点。
public class testthread{ public static void main(){ Runner1 r = new Runner1(); Thread t = new Thread(r); t.start;//主线程休眠10秒 try(Thread.sleep(100000);){} catch(interruptedException e){} //醒来后让子线程抛interruptedException导常,这个指子线程在休眠时被外部线程时调用则一定抛此异常。 thread.interrup(); } class Runner1 implements Runnable{ public void run(){ //while(true)代表无限循环 while(true){ …………………… try(sleep(10000)){ }catch (interruptedException e){ return;//被主线程在休眠时调用,进入catch 然后return,子线程就被结束了。其实在catch时还可以写点别的东西,看具体业务而定。 } } } }}