停止java线程的方法及详解

在java中有三个方法可以终止正在运行的程序:

         1.使用退出标志,使线程正常退出,也就是当run方法完成后线程终止

         2.使用stop方法强行终止线程,但是不推荐使用这种方法,因为stop和suspend及resume一样,都是作过期的方法,使用它们可能产生不可预料的结果

         3.使用interrupt方法中断线程


使用interrupt方法中止线程:

先补充一些基本概念:

        -----需要花一些时间的方法:

              执行wait方法的线程,会进入等待区等待notify/notigy all。在等待期间,线程不会运动

              执行sleep方法的线程,会执行参数内所设置的时间

              执行join方法的线程,会等待到指定的线程结束为止

        ------可以去取消的方法:

               因为需要花时间的操作会降低程序的响应性,所以可能会取消/中途放弃执行这个方法

               这里主要通过interrupt方法来取消

        sleep方法和Interrupt方法:

               interrupt方法是Thread类的实例方法,在执行的时候并不需要获取Thread实例的锁定,任何线程在任何时刻,都可以通过线程实例来调用其他线程的interrupt方法。

              当在sleep中的线程调用了interrupt方法时,就会放弃暂停状态了,并抛出InterruptedException异常,这样一来,线程的控制权就交给了捕捉这个异常的catch快了

        wait和interrupt方法

              当线程调用wait方法后,线程在进入等待区时,会把锁定解除。当对wait中的线程调用interrupt方法时,会先重新获取锁定,再抛出InterruptedException异常,获取锁定之前,无法抛出InterruptedException异常。

         join和interrupt方法:

               当线程以join方法等待其他线程结束时,一样可以使用interrupt方法取消。因为join方法不需要获取锁定,故而与sleep一样,会马上跳到catch程序块

        interrupt方法做了什么

               interrupt方法其实只是改变了中断状态而已

               而sleep\wait和join这些方法的内部会不断的检查中断状态的值,从而自己抛出InterruptedException异常

               所以,如果在线程进行其他处理时,调用了它的interrupt方法,线程也不会抛出InterruptedException的,只有当线程走到了sleep, wait, join这些方法的时候,才会抛出InterruptedException。若是没有调用sleep, wait, join这些方法,或者没有在线程里自己检查中断状态,自己抛出InterruptedException,那InterruptedException是不会抛出来的。

               isInterrupted方法,用来检查中断状态。

               Thread.interrupted方法,可以用来检查并清除中断状态。

        使用interrupt停止线程1;

           例子一:用for来判断一下线程是否处于停止状态:

class Mythread extends Thread{
	public void run(){
		super.run();
		for(int i=0;i<5000000;i++){
			if(this.isInterrupted()){
				System.out.println("已经是停止状态了,我要退出了");
				break;
			}
			System.out.println("i="+(i+1));
		}
	}
}

public class test {
	public static void main(String[] args) throws Exception{
		try{
			Mythread mythread=new Mythread();
			mythread.start();
			Thread.sleep(1000);
			mythread.interrupt();
		}catch(Exception e){
			System.out.println("main catch");
			e.printStackTrace();
		}
	}
}
上边的例子虽然停止了线程,但是for语句还有下边的语句会继续运行的,
class Mythread extends Thread{
	public void run(){
		super.run();
		for(int i=0;i<5000000;i++){
			if(this.isInterrupted()){
				System.out.println("已经是停止状态了,我要退出了");
				break;
			}
			System.out.println("i="+(i+1));
		}
		System.out.println("我在下边呦");
	}
}

public class test {
	public static void main(String[] args) throws Exception{
		try{
			Mythread mythread=new Mythread();
			mythread.start();
			Thread.sleep(1000);
			mythread.interrupt();
		}catch(Exception e){
			System.out.println("main catch");
			e.printStackTrace();
		}
	}
}
如何解决语句继续运行的问题呢,
class Mythread extends Thread{
	public void run(){
		super.run();
		try{
			for(int i=0;i<5000000;i++){
				if(this.isInterrupted()){
					System.out.println("已经是停止状态了,我要退出了");
					throw new InterruptedException();
				}
				System.out.println("i="+(i+1));
			}
			System.out.println("我在下边呦");
		}catch(InterruptedException e){
			e.printStackTrace();
		}
	}
}

public class test {
	public static void main(String[] args) throws Exception{
		try{
			Mythread mythread=new Mythread();
			mythread.start();
			Thread.sleep(1000);
			mythread.interrupt();
		}catch(Exception e){
			System.out.println("main catch");
			e.printStackTrace();
		}
	}
}

在沉睡中停止:

先睡在终止

class Mythread extends Thread{
	public void run(){
		super.run();
		try{
			System.out.println("run begin");
			Thread.sleep(2000);
			System.out.println("run end");
		}catch(InterruptedException e){
			e.printStackTrace();
		}
	}
}

public class test {
	public static void main(String[] args) throws Exception{
		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!");
	}
}

先终止再睡:

class Mythread extends Thread{
	public void run(){
		super.run();
		try{
			for(int i=0;i<10;i++){
				System.out.println("i="+(i+1));
			}
			System.out.println("run begin");
			Thread.sleep(2000);
			System.out.println("run end");
		}catch(InterruptedException e){
			System.out.println("先停止,再遇到了sleep,进入catch");
			e.printStackTrace();
		}
	}
}

public class test {
	public static void main(String[] args) throws Exception{
		try{
			Mythread mythread=new Mythread();
			mythread.start();
			mythread.interrupt();
			System.out.println("end");
		}catch(Exception e){
			System.out.println("main catch");
			e.printStackTrace();
		}
	}
}

暴力停止一个线程-----调用stop方法:

        调用一个stop方法会抛出java.lang.ThreadDeath异常,但是在通常的情况下,此异常不需要显示的捕捉。

        方法stop已经被作废,因为如果强制让线程停止则有可能使一些请理性的工作得不到完成,另一个情况就是对锁定的对象进行了解锁,导致数据得不到同步的处理,出现数据不一致的问题。

        使用stop()释放锁,会给数据造成不一致的结果,如果出现这种情况,程序处理的数据就会有可能破坏,最终导致程序的执行流程错误,一定要特别注意。

       

class SynchronizedObject{
	String name="zhangsan";
	String password="123";
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	synchronized public void printString(String name,String password){
		try {
			this.name=name;
			Thread.sleep(2000);
			this.password=password;
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
class Mythread extends Thread{
	private SynchronizedObject object;
	public Mythread(SynchronizedObject object){
		this.object=object;
	}
	public void run(){
		super.run();
		object.printString("t", "tt");
	}
}

public class test {
	public static void main(String[] args) throws Exception{
		SynchronizedObject obj=new SynchronizedObject();
		Mythread mythread=new Mythread(obj);
		Thread.sleep(200);
		mythread.start();
		System.out.println(obj.getName()+"---"+obj.getPassword());
	}
}
输出结果:t---123

使用return停止线程:

class Mythread extends Thread{
	public void run(){
		while(true){
			if(this.isInterrupted()){
				System.out.println("停止了");
				return;
			}
			System.out.println("time is -->"+System.currentTimeMillis());;
		}
	}
}

public class test {
	public static void main(String[] args) throws Exception{
		Mythread mythread=new Mythread();
		mythread.start();
		Thread.sleep(200);
		mythread.interrupt();
		
	}
}
建议使用“抛异常”方式来完成线程的停止,因为在catch块中可以对异常的信息进行相关的处理,而且使用异常流能更好,更能方便的控制程序的运行流程,不至于代码中出现国哥return,造成污染。






     

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值