首页 / JAVA / java多线程之停止线程
java多线程之停止线程
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java多线程之停止线程,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5365字,纯文字阅读大概需要8分钟。
内容图文
在多线程开发中停止线程是很重要的技术点。停止线程在Java语言中并不像break语句那样干脆,需要一些技巧性的处理。
一、 异常法
采用异常法来停止一个线程,首先我们需要了解一下两个方法的用法:
1、interrupt()方法
public class MyThread extends Thread{ @Override public void run() { for (int i = 1; i <= 10000; i++) { System.out.println("i="+i); } } public static void main(String[] args)throws Exception { MyThread thread=new MyThread(); thread.start(); thread.sleep(10); thread.interrupt(); } }
上面的例子调用interrupt()方法来停止线程,但interrupt()方法的使用效果并不像for+break语句那样,马上就能停止循环。调用interrupt()方法仅仅是在当前线程打了一个停止的标记,并不是真的停止。那么如果停止线程了?我们接着往下面看。
2、判断线程是否是停止状态
1)、 interrupted()
public class MyThread extends Thread{ @Override public void run() { for (int i = 1; i <= 10000; i++) { System.out.println("i="+i); } } public static void main(String[] args)throws Exception { try{ MyThread thread=new MyThread(); thread.start(); thread.sleep(100); thread.interrupt(); System.out.println("线程停止了吗1?--->"+thread.interrupted()); System.out.println("线程停止了吗2?--->"+thread.interrupted()); }catch(Exception e){ e.printStackTrace(); } System.out.println("end"); } }
从控制台打印的结果来看,线程没有停止,这就是说,interrupted()测试当前线程是否中断,因为这个当前线程就是main,它没有中断过,所以打印的结果是两个false。
public class MyThread{ public static void main(String[] args){ Thread.currentThread().interrupt(); System.out.println("线程停止了吗1?---->"+Thread.interrupted()); System.out.println("线程停止了吗2?---->"+Thread.interrupted()); System.out.println("end"); } }
2)、 isInterrupted()
public class MyThread extends Thread{ @Override public void run() { for (int i = 1; i <= 10000; i++) { System.out.println("i="+i); } } public static void main(String[] args){ try{ MyThread thread=new MyThread(); thread.start(); thread.sleep(10); thread.interrupt(); System.out.println("线程停止了吗1?--->"+thread.isInterrupted()); System.out.println("线程停止了吗2?--->"+thread.isInterrupted()); }catch(Exception e){ e.printStackTrace(); } System.out.println("end"); } }
3、停止线程
public class MyThread extends Thread{ @Override public void run() { for (int i = 1; i <= 10000; i++) { if(this.interrupted()){ System.out.println("线程是停止状态了,我要退出了."); break; } System.out.println("i="+i); } System.out.println("如果此处还是循环,那么我就会继续执行.线程并没有停止"); } public static void main(String[] args){ try{ MyThread thread=new MyThread(); thread.start(); thread.sleep(10); thread.interrupt(); System.out.println("end"); }catch(Exception e){ e.printStackTrace(); } } }
public class MyThread extends Thread{ @Override public void run() { try{ for (int i = 1; i <= 10000; i++) { if(this.interrupted()){ System.out.println("线程是停止状态了,我要退出了."); throw new InterruptedException(); } System.out.println("i="+i); } System.out.println("我被执行了吗?"); }catch(InterruptedException e){ System.out.println("---这次线程停了---"); e.printStackTrace(); } } public static void main(String[] args){ try{ MyThread thread=new MyThread(); thread.start(); thread.sleep(10); thread.interrupt(); System.out.println("end"); }catch(Exception e){ e.printStackTrace(); } } }
二、 在沉睡中停止
public class MyThread extends Thread{ @Override public void run() { try{ System.out.println("run start"); Thread.sleep(1000000); System.out.println("run end"); }catch(InterruptedException e){ System.out.println("sleep被停止,状态:--->"+this.isInterrupted()); e.printStackTrace(); } } public static void main(String[] args){ try{ MyThread thread=new MyThread(); thread.start(); thread.interrupt(); thread.sleep(1000); }catch(Exception e){ System.out.println("main catch"); e.printStackTrace(); } } }
public class MyThread extends Thread{ @Override public void run() { try{ for (int i = 1; i <= 10000; i++) { System.out.println("i="+i); } System.out.println("run start"); Thread.sleep(1000000); System.out.println("run end"); }catch(InterruptedException e){ System.out.println("线程被停止了,在sleep,状态:--->"+this.isInterrupted()); e.printStackTrace(); } } public static void main(String[] args){ try{ MyThread thread=new MyThread(); thread.start(); thread.interrupt(); }catch(Exception e){ System.out.println("main catch"); e.printStackTrace(); } } }
三、 暴力停止
public class MyThread extends Thread{ private int i=0; @Override public void run() { try{ while (true) { i++; System.out.println("i="+i); Thread.sleep(1000); } }catch(Exception e){ e.printStackTrace(); } } public static void main(String[] args){ try{ MyThread thread=new MyThread(); thread.start(); Thread.sleep(6000); thread.stop(); }catch(Exception e){ e.printStackTrace(); } } }
四、 使用return停止线程
public class MyThread extends Thread{ private int i=0; @Override public void run() { try{ while (true) { i++; if(this.interrupted()){ System.out.println("线程停止了"); return; } System.out.println("i="+i); } }catch(Exception e){ e.printStackTrace(); } } public static void main(String[] args){ try{ MyThread thread=new MyThread(); thread.start(); Thread.sleep(2000); thread.interrupt(); }catch(Exception e){ e.printStackTrace(); } } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/luckey_zh/article/details/46955739
内容总结
以上是互联网集市为您收集整理的java多线程之停止线程全部内容,希望文章能够帮你解决java多线程之停止线程所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。