本文主要总结在java中停止线程的方法在java中有以下三种方法可以终止正在运行的线程:1、使用退出标志2、使用stop方法强行终止线程,但是不推荐,因为stop和suspend、resume一样都是过时的方法3、使用interrup方法中断线程停止不了的线程本例将使用inter ...
本文主要总结在java中停止线程的方法
在java中有以下三种方法可以终止正在运行的线程:
1、使用退出标志
2、使用stop方法强行终止线程,但是不推荐,因为stop和suspend、resume一样都是过时的方法
3、使用interrup方法中断线程
停止不了的线程
本例将使用interrupt方法来停止进程,看看效果:
public class MyThread extends Thread { @Override public void run() { super.run(); for (int i = 0; i < 1234; i++) { System.out.println("i=" + (i + 1)); } }}public class Run { public static void main(String[] args) { try { MyThread thread = new MyThread(); thread.start(); Thread.sleep(2000); thread.interrupt(); } catch (InterruptedException e) { System.out.println("main catch"); e.printStackTrace(); } }}
海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com
原标题:java多线程系列7
关键词:JAVA
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。