多线程——————停止线程
来源:互联网 发布:中国原油进口数据 编辑:程序博客网 时间:2024/06/08 09:34
下面我们来讲一下:多线程——————停止线程
停止线程的主要方法:
- Thread.interrupt():不是中断线程,而是通知线程【应该中断了】,具体到底是否中断还是运行由用户判断决定
- interrupted():测试当前线程是否中断。线程的中断状态由该方法清除,如果连续两次调用该方法,第二次调用会返回flase
- isInterrupted():也是测试线程是否中断,但不会清除状态
注意点:
1.如果在sleep()状态下停止某一线程,会进入catch语句,并清除停止状态值,变为flase
2.停止线程再遇见sleep(),也会进入cathch语句
3.线程停止后面语句是否执行?
4.使用return停止线程
下面我们来看例子:
public class test4 extends Thread { @Override public void run() { for (int i = 0; i < 500000; i++) { System.out.println("i="+(i+1)); } } public static void main(String[] args) { test4 t=new test4(); t.start(); try { Thread.sleep(50); } catch (InterruptedException e) { System.out.println("main catch"); e.printStackTrace(); } //停止当前线程 t.interrupt(); }}//通知线程应该中断,是否中断由用户决定
我们发现实际上线程并未停止
public class test5 extends Thread { @Override public void run() { for (int i = 0; i < 100; i++) { System.out.println("i="+(i+1)); } } public static void main(String[] args) { try { test5 t=new test5(); t.start(); Thread.sleep(1000); System.out.println("是否停止1?="+t.interrupted()); System.out.println("是否停止2?="+t.interrupted()); } catch (InterruptedException e) { System.out.println("main catch"); e.printStackTrace(); } System.out.println("-----------------------------------"); //main 线程 Thread.currentThread().interrupt(); System.out.println("是否停止1?="+Thread.interrupted()); System.out.println("是否停止2?="+Thread.interrupted()); System.out.println("end"); }}//interrupted会清除线程状态
public class test6 extends Thread { @Override public void run() { for (int i = 0; i < 500000; i++) { if(this.interrupted()){ System.out.println("已经是停止状态"); break; } System.out.println("i="+(i+1)); } } public static void main(String[] args) { try { test6 t=new test6(); t.start(); Thread.sleep(2000); t.interrupt(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("end"); }}//判断后中断
public class test7_0 extends Thread { @Override public void run() { for (int i = 0; i < 500000; i++) { if(this.interrupted()){ System.out.println("已经是停止状态,我要退出了"); break; } System.out.println("i="+(i+1)); } System.out.println("我要退出了,如果此代码是for又继续运行,线程并未停止"); } public static void main(String[] args) { try { test7_0 t = new test7_0(); t.start(); Thread.sleep(2000); t.interrupt(); } catch (Exception e) { } }}//虽然已经中断了线程,但是for循环外后面的语句正常执行
public class test7 extends Thread { @Override public void run() { try { for (int i = 0; i < 500000; i++) { if (this.interrupted()) { System.out.println("已经是停止状态,我要退出了"); throw new InterruptedException(); } System.out.println("i="+(i+1)); } System.out.println("我在for下面,但是没有执行"); } catch (Exception e) { System.out.println("进run方法中的catch中了"); e.printStackTrace(); } } public static void main(String[] args) { try { test7 t = new test7(); t.start(); Thread.sleep(2000); t.interrupt(); } catch (Exception e) { System.out.println("main catch"); e.printStackTrace(); } System.out.println("end"); }}//抛出异常,直接进入catch块
public class test8 extends Thread { @Override public void run() { try { System.out.println("run begin"); Thread.sleep(200000); System.out.println("run end"); } catch (InterruptedException e) { System.out.println("在沉睡中被停止!进入catch "+this.isInterrupted()); e.printStackTrace(); } } public static void main(String[] args) { try { test8 t = new test8(); t.start(); Thread.sleep(200); t.interrupt(); } catch (Exception e) { System.out.println("main catch"); e.printStackTrace(); } System.out.println("end"); }}// 如果sleep状态下停止某一个线程,会进入catch语句, 并清除停止状态值,使之变为flase
public class test9 extends Thread { @Override public void run() { try { for (int i = 0; i < 500000; i++) { System.out.println("i=" + (i + 1)); } System.out.println("run begin"); Thread.sleep(200000); System.out.println("run end"); } catch (Exception e) { System.out.println("先停止,在遇到了sleep!进入catch"); e.printStackTrace(); } } public static void main(String[] args) { test9 t=new test9(); t.start(); t.interrupt(); System.out.println("end"); }}// 先停止线程,再遇见sleep! 也会进入catch语句
public class test1 extends Thread { @Override public void run() { while (true) { if (this.isInterrupted()) { System.out.println("停止了"); return; } System.out.println("timer=" + System.currentTimeMillis()); } } public static void main(String[] args) { try { test1 t = new test1(); t.start(); Thread.sleep(2000); t.interrupt(); } catch (Exception e) { } }}//不过还是建议使用"抛异常"的方法来实现线程的停止,因为catch块中还可以将异常向上抛 使线程停止的事件得以传播
阅读全文
0 0
- 多线程——停止和守护线程
- Java多线程学习笔记—线程停止
- [线程]——停止线程
- 多线程——————停止线程
- java基础——多线程——线程停止
- Java多线程3——如何停止线程
- java多线程学习笔记(八) ——线程停止(interrupt)
- Java多线程初探——正确停止线程
- 多线程——停止线程(结束run方法)
- Java多线程学习(1)——停止线程
- 1.7 JAVA多线程编程——停止线程
- 27、Java入门—多线程之线程的正确停止
- JAVA之旅(十五)——多线程的生产者和消费者,停止线程,守护线程,线程的优先级,setPriority设置优先级,yield临时停止
- 多线程——停止线程(intertupt方法setDaemon和方法的使用)
- Java——线程正常停止
- Java——线程的停止
- 黑马程序员——008——多线程②(线程间通信(同步,锁)、停止线程)
- java线程学习3——线程的停止
- nexus搭建maven私服及私服jar包上传和下载
- android 6.0 SystemUI源码分析(1)-SystemUI介绍
- A child container failed during start java.util.concurrent.ExecutionException异常解决
- cf 421 (2 of 5)
- 各大公司后端java面试题总结
- 多线程——————停止线程
- 在shiro当设置sessionIdCookie配置的domain和访问url不匹配时,每次请求都会重新生成session
- HTML 标签解释大全
- 学习Linux命令(51)
- 3.Longest Substring Without Repeating Characters-python
- Android强大的数据库开源框架LitePal
- Ceph新长支持稳定版本Luminous(12.x.x)新功能总结
- CSS入门
- toad 启动时长时间 checking feedback server