停止线程之异常法以及在沉睡中停止线程

一:停止线程之异常法

  1.  简介
       我们推荐使用interrupt()方法来停止线程,但是单纯调用interrupt()方法仅仅是在当前线程中打了一个停止的标记,
       并没有真的停止线程。为了解决这样的问题,我们使用了抛出异常的方法。
  2. 创建继承Thread类的线程类MyThread.java
      
    package com.kgf.test;
    
    public class MyThread extends Thread {
    
    	@SuppressWarnings("static-access")
    	@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("进入catch方法了。。。。。。");
    			e.printStackTrace();
    		}
    	}
    	
    }
    

     

  3. 测试类
        
    package com.kgf.test;
    
    public class Run {
    
    	public static void main(String[] args) throws InterruptedException {
    		try {
    			MyThread myThread = new MyThread();
    			myThread.start();
    			Thread.sleep(1000); //此方法代表 让当前线程休眠 1 秒,即表示使 main线程休眠 1秒
    			myThread.interrupt();//调用停止线程类的方法
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		System.out.println("==========END================");
    	}
    }
    

     

  4. 效果
         
  5. 总结
      建议使用“抛异常”方式来实现线程的停止,因为在catch块中可以对异常信息进行相关的处理
      ,而且使用异常流能更好,更方便的控制程序的运行流程。
                 

二:在沉睡中停止线程

  1. 先sleep再用interrupt()停止
     ⑴创建继承Thread类的线程类MyThread.java
         
    package com.kgf.test;
    
    public class MyThread extends Thread {
    
    	@Override
    	public void run() {
    		try {
    			System.out.println("===run begin=====");
    			Thread.sleep(200000);
    			System.out.println("===run end=====");
    		} catch (Exception e) {
    			System.out.println("在沉睡中被停止!进入catch!线程状态为:"+this.isInterrupted());
    			e.printStackTrace();
    		}
    	}
    	
    }
    

     ⑵测试类
        
    package com.kgf.test;
    
    public class Run {
    
    	public static void main(String[] args) throws InterruptedException {
    		try {
    			MyThread myThread = new MyThread();
    			myThread.start();
    			Thread.sleep(200);
    			myThread.interrupt();//调用停止线程类的方法
    		} catch (Exception e) {
    			System.out.println("main catch");
    			e.printStackTrace();
    		}
    		System.out.println("==========END================");
    	}
    }
    

     ⑶效果:
        

     ⑷结果分析
         通过上面的代码我们可知,我们是先让MyThread对象线程进入到睡眠状态,然后
       我们再调用interrupt()方法去停止它。从结果看,如果在sleep状态下停止某一线程,
       就会使其进入catch语句,并且清除停止状态值,使其变成false.       
  2. 先interrupt()停止再sleep
     ⑴创建继承Thread类的线程类MyThread.java
       
    package com.kgf.test;
    
    public class MyThread 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!线程状态为:"+this.isInterrupted());
    			e.printStackTrace();
    		}
    	}
    	
    }
    

     ⑵测试类 
        
    package com.kgf.test;
    
    public class Run {
    
    	public static void main(String[] args) throws InterruptedException {
    		try {
    			MyThread myThread = new MyThread();
    			myThread.start();
    			myThread.interrupt();//调用停止线程类的方法
    		} catch (Exception e) {
    			System.out.println("main catch");
    			e.printStackTrace();
    		}
    		System.out.println("==========END================");
    	}
    }
    

     ⑶效果
        

     ⑷结果分析
        从上面结果可知,当我们先执行interrupt()方法时,myThread线程会一直运行到sleep()方法
       时才进入catch语句中,不会立刻停止。  
              
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值