网站开发及后期维护,wordpress如何添加导航,无法进入wordpress,搭建电商网站通常我们会有这样的需求#xff0c;即停止一个线程。在java的api中有stop、suspend等方法可以达到目的#xff0c;但由于这些方法在使用上存在不安全性#xff0c;会带来不好的副作用#xff0c;不建议被使用。具体原因可以参考Why is Thread.stop deprecated。在本文中即停止一个线程。在java的api中有stop、suspend等方法可以达到目的但由于这些方法在使用上存在不安全性会带来不好的副作用不建议被使用。具体原因可以参考Why is Thread.stop deprecated。在本文中将讨论中断在java中的使用。中断在java中主要有3个方法interrupt(),isInterrupted()和interrupted()。interrupt()在一个线程中调用另一个线程的interrupt()方法即会向那个线程发出信号——线程中断状态已被设置。至于那个线程何去何从由具体的代码实现决定。isInterrupted()用来判断当前线程的中断状态(true or false)。interrupted()是个Thread的static方法用来恢复中断状态名字起得额。接下来看看具体在代码中如何使用。interrupt()不能中断在运行中的线程它只能改变中断状态而已。public class InterruptionInJava implementsRunnable{public static void main(String[] args) throwsInterruptedException {Thread testThread new Thread(new InterruptionInJava(),InterruptionInJava);//start threadtestThread.start();Thread.sleep(1000);//interrupt threadtestThread.interrupt();System.out.println(main end);}Overridepublic voidrun() {while(true){if(Thread.currentThread().isInterrupted()){System.out.println(Yes,I am interruted,but I am still running);}else{System.out.println(not yet interrupted);}}}}结果显示被中断后仍旧运行不停打印Yes,I am interruted,but I am still running那么如何正确中断既然是只能修改中断状态那么我们应该针对中断状态做些什么。public class InterruptionInJava implementsRunnable{public static void main(String[] args) throwsInterruptedException {Thread testThread new Thread(new InterruptionInJava(),InterruptionInJava);//start threadtestThread.start();//Thread.sleep(1000);//interrupt threadtestThread.interrupt();System.out.println(main end);}Overridepublic voidrun() {while(true){if(Thread.currentThread().isInterrupted()){System.out.println(Yes,I am interruted,but I am still running);return;}else{System.out.println(not yet interrupted);}}}}修改代码在状态判断中如上添加一个return就okay了。但现实中我们可能需要做的更通用不禁又要发出天问如何中断线程答案是添加一个开关。public class InterruptionInJava implementsRunnable{private volatile static boolean on false;public static void main(String[] args) throwsInterruptedException {Thread testThread new Thread(new InterruptionInJava(),InterruptionInJava);//start threadtestThread.start();Thread.sleep(1000);InterruptionInJava.on true;System.out.println(main end);}Overridepublic voidrun() {while(!on){if(Thread.currentThread().isInterrupted()){System.out.println(Yes,I am interruted,but I am still running);}else{System.out.println(not yet interrupted);}}}}这表明是成功中断了的这种开关的方式看起来包治百病但是当遇到线程阻塞时就会很无奈了正如下面代码所示public class InterruptionInJava implementsRunnable{private volatile static boolean on false;public static void main(String[] args) throwsInterruptedException {Thread testThread new Thread(new InterruptionInJava(),InterruptionInJava);//start threadtestThread.start();Thread.sleep(1000);InterruptionInJava.on true;System.out.println(main end);}Overridepublic voidrun() {while(!on){try{Thread.sleep(10000000);}catch(InterruptedException e) {System.out.println(caught exception: e);}}}}线程被阻塞无法被中断。这时候救世主interrupt函数又回来了它可以迅速中断被阻塞的线程抛出一个InterruptedException把线程从阻塞状态中解救出来,show the code。public class InterruptionInJava implementsRunnable{private volatile static boolean on false;public static void main(String[] args) throwsInterruptedException {Thread testThread new Thread(new InterruptionInJava(),InterruptionInJava);//start threadtestThread.start();Thread.sleep(1000);InterruptionInJava.on true;testThread.interrupt();System.out.println(main end);}Overridepublic voidrun() {while(!on){try{Thread.sleep(10000000);}catch(InterruptedException e) {System.out.println(caught exception right now: e);}}}这种情形同样适用io阻塞通常io阻塞会立即抛出一个SocketException类似于上面说的InterruptedException。