java线程中断 interrupt详解

概述:

java在运行线程的时候,我们应该有效的停止线程的运行。比如一个Activity中有个线程是无限循环的,假如每次退出activity的时候,不能有效的关闭线程的话(Activity销毁,分线程还是存在的),那么每次再次进入Activity的时候,都会创建新的分线程,这样越来越多的分线程大量的消耗者CPU资源,从而导致程序运行越来越慢,所以我们要有效的关闭分线程的运行。

一、正常运行一段代码

先运行一段代码:

class ATask implements Runnable{

	private double d = 0.0;
	
	public void run() {
		//死循环执行打印"I am running!" 和做消耗时间的浮点计算
		while (true) {
			System.out.println("I am running!");
			
			for (int i = 0; i < 900000; i++) {
				d =  d + (Math.PI + Math.E) / d;
			}
			//给线程调度器可以切换到其它进程的信号
			Thread.yield();
		}
	}
}

public class InterruptTaskTest {
	
	public static void main(String[] args) throws Exception{
		//将任务交给一个线程执行
		Thread t = new Thread(new ATask());
		t.start();
		
		//运行一断时间中断线程
		Thread.sleep(100);
		System.out.println("****************************");
		System.out.println("Interrupted Thread!");
		System.out.println("****************************");
		t.interrupt();
	}
} 

运行结果:

......
I am running!
I am running!
I am running!
I am running!
****************************
Interrupted Thread!
****************************
I am running!
I am running!
I am running!
I am running!
I am running!
....

运行这个程序,我们发现调用interrupt()后,程序仍在运行,如果不强制结束,程序将一直运行下去。

也就是线程并没有停止运行,所以我们单独的调用interrupt()方法是不可以的。

二、当线程等待某些事件发生而被阻塞,又会发生什么?当然,如果线程被阻塞,它便不能核查共享变量,也就不能停止。这在许多情况下会发生,例如调用Object.wait()ServerSocket.accept()DatagramSocket.receive()时,这里仅举出一些。
他们都可能永久的阻塞线程。即使发生超时,在超时期满之前持续等待也是不可行和不适当的,所以,要使用某种机制使得线程更早地退出被阻塞的状态。

1、分线程如果处于Object.wait, Thread.joinThread.sleep三种方法之一阻塞的情况下,我们调用interrupt()方法的话,就会抛出中断异常InterruptedException),从而提早地终结被阻塞状态。

class ATask implements Runnable{

	private double d = 0.0;
	
	public void run() {
		//死循环执行打印"I am running!" 和做消耗时间的浮点计算
		try {
			while (true) {
				System.out.println("I am running!");
				
				for (int i = 0; i < 900000; i++) {
					d =  d + (Math.PI + Math.E) / d;
				}
				//休眠一断时间,中断时会抛出InterruptedException
				Thread.sleep(50);
			}
		} catch (InterruptedException e) {
			System.out.println("ATask.run() interrupted!");
		}
	}
}
因此,如果线程被上述几种方法阻塞,正确的停止线程方式是设置共享变量,并调用 interrupt() (注意变量应该先设置)。如果线程没有被阻塞,这时调用 interrupt() 将不起作用;否则,线程就将得到异常(该线程必须事先预备好处理此状况),接着逃离阻塞状态。在任何一种情况中,最后线程都将检查共享变量然后再停止。
boolean flag = true;
class ATask implements Runnable{

	private double d = 0.0;
	
	public void run() {
		//死循环执行打印"I am running!" 和做消耗时间的浮点计算
		try {
			while (flag) {
				System.out.println("I am running!");
				
				for (int i = 0; i < 900000; i++) {
					d =  d + (Math.PI + Math.E) / d;
				}
				//休眠一断时间,中断时会抛出InterruptedException
				Thread.sleep(50);
			}
		} catch (InterruptedException e) {
			System.out.println("ATask.run() interrupted!");
		}
	}
}
如果要停止线程的话,就设置flag=false,当循环到flag为false的话,结束线程。
2、以上说的是被 Object.wait, Thread.join Thread.sleep这些阻塞的情况,那么假如是Socket阻塞和I/O阻塞的话又是什么情况呢?

运行一段代码:

public class BlockingThreadTest {
	static boolean flag = true;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Service mService = new Service();
		Thread t = new Thread(mService);
		t.start();
		
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		flag = false;
		t.interrupt();
	}
	
	
	
	static class Service implements Runnable{

		ServerSocket mServerSocket;
		@Override
		public void run() {
			try {
				mServerSocket = new ServerSocket(5020);
				while(flag){
					System.out.println("接收socket");
					Socket socket = mServerSocket.accept();
					
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

我们通过
flag = false;
t.interrupt();
这两种方式试图中断Thread,但是还是没有起到作用,那么我们应该用什么方式呢?

public class BlockingThreadTest {
	static boolean flag = true;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Service mService = new Service();
		Thread t = new Thread(mService);
		t.start();

		try {
			Thread.sleep(3000);

			flag = false;
			t.interrupt();
			mService.mServerSocket.close();

		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (IOException e) {
			
		}

	}

	static class Service implements Runnable {

		ServerSocket mServerSocket;

		@Override
		public void run() {
			try {
				mServerSocket = new ServerSocket(5020);
				while (flag) {
					System.out.println("接收socket");
					Socket socket = mServerSocket.accept();

				}
			} catch (IOException e) {
				System.out.println("线程阻塞被结束,线程结束");
			}
		}
	}
}
对就是用了
mService.mServerSocket.close();
通过关闭Socket来切断阻塞,从而终止线程


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值