Thread类之线程中断

Java的Thread类中有三个与线程中断有关的方法,这三个方法相对来说还是比较简单的,稍稍介绍一下这三个方法之间的差别:

  • public void Thread.interrupt()    --中断线程
  • public boolean Thread.isInterrupted()    --判断当前线程是否被中断
  • public boolean Thread.interrupted()    --判断当前线程是否被中断,并清除当前中断状态


接下来用三个小例子来说明一下这三个方法的运用场景:

/**
 * Thread类中的interrutp方法并不一定百分百中断线程
 * jdk的api中有这么一句话:如果当前线程没有中断它自己(这在任何情况下都是允许的)
 * 并且api中说明:如果线程调用 Object类中的 wait、join等一系列阻塞方法时,
 * interrutp()才会致使线程中断,并抛出一个InterruptedException。 
 * 注:该程序必须要在jvm的server模式下运行,在client模式下运行看不到效果
 * 有关jvm的server模式与client模式有兴趣的朋友可以到网上查找相关资料,都解释的比较详细
 * 如果使用的是jdk7即以上版本,则默认就是使用server模式,不需要进行额外设置
 */
public class FirstInterruptDemo extends Thread 
{
	public void run()
	{
		while(true)
		{
			System.out.println("线程运行中....");
			//让主程序能够执行中断
			Thread.yield();
		}
	}
	
	
	public static void main(String[] args)
	{
		Thread t = new FirstInterruptDemo();
		t.start();
		//去中断线程,看线程是否会被中断
		t.interrupt();
		System.out.println("==========中断已执行==========");
	}
}

其运行结果如下:可以看到,即使执行了线程中断,但是线程依旧处于运行状态,并没有停止下来

小提示:尽可能快速的关闭虚拟机,否则执行时间过长,由于控制台能够显示的行数有限,可能看不到“中断已执行这句话的输出”




/**
 * Thread类中的interrutp方法并不一定百分百中断线程
 * jdk的api中有这么一句话:如果当前线程没有中断它自己(这在任何情况下都是允许的)
 * 并且api中说明:如果线程调用嗯啦 Object类中的 wait、join等一系列阻塞方法时,
 * interrutp()才会致使线程中断,并抛出一个InterruptedException。 
 * 在线程未阻塞的情况下,interrutp()更像一个通知,或者说标记,告诉该线程要中断
 * 需要用isInterrupted()方法判断线程是否有这样一个中断标记
 *
 */
public class SecondInterruptDemo extends Thread
{
	public void run()
	{
		while(true)
		{
			if(Thread.currentThread().isInterrupted())
			{
				System.out.println("========线程退出========");
				break;
			}
			System.out.println("线程运行中....");
			//让主线程能够执行中断
			Thread.yield();
		}
	}
	
	public static void main(String[] args)throws Exception
	{
		Thread t = new SecondInterruptDemo();
		t.start();
		//让线程有执行
		Thread.sleep(50);
		//去中断线程,看线程是否会被中断
		t.interrupt();
		System.out.println("==========中断已执行==========");
	}
}


其运行结果如下:



/**
 * Thread类中的interrutp方法并不一定百分百中断线程
 * jdk的api中有这么一句话:如果当前线程没有中断它自己(这在任何情况下都是允许的)
 * 并且api中说明:如果线程调用嗯啦 Object类中的 wait、join等一系列阻塞方法时, 
 * 
 * 因为线程处于阻塞状态,调用interrutp会致使线程中断,并抛出一个InterruptedException。
 * 可在catch中执行退出线程的操作
 */
public class ThirdInterruptDemo extends Thread
{
	public void run()
	{
		synchronized (this)
		{
			while(true)
			{
				try 
				{
					System.out.println("线程执行中");
					this.wait();
				} 
				catch (InterruptedException e)
				{
					System.out.println("线程退出");
					break;
				}
			}
		}
	}
	
	public static void main(String[] args)throws Exception
	{
		Thread t = new ThirdInterruptDemo();
		t.start();
		//让线程有执行
		Thread.sleep(50);
		//去中断线程,看线程是否会被中断
		t.interrupt();
		System.out.println("==========中断已执行==========");
	}
}

执行结果如下:



前面有提到过isInterrupted()方法与interrupted()方法之间的区别:

public boolean Thread.isInterrupted()    --判断当前线程是否被中断
public boolean Thread.interrupted()    --判断当前线程是否被中断,并清除当前中断状态


/**
 *	Thread.interrupted()测试当前线程是否已经中断。线程的中断状态 由该方法清除。
 *	换句话说,如果连续两次调用该方法,则第二次调用将返回 false(在第一次调用已清除了其中断状态之后,且第二次调用检验完中断状态前,当前线程再次中断的情况除外)。 
 */
public class FouthInterruptDemo extends Thread
{
	public void run()
	{
		for(int i = 0; i < 5; i++)
		{
			//让主线程能够执行中断
			Thread.yield();
			System.out.println(Thread.interrupted());
                        //System.out.println(Thread.currentThread().isInterrupted());
               }
	}
	
	public static void main(String[] args)
	{
		Thread t = new FouthInterruptDemo();
		t.start();
		//执行中断
		t.interrupt();
	}
}

其运行结果如下:

感兴趣的朋友可以将Thread.interrupted()换成Thread.isInterrupt()看看结果有什么不同


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值