Java多线程(4):interrupt() interrupted() isInterrupted()

interrupt():中断线程(设置中断标识位)

interrupted():静态方法 测试当前线程是否已经中断(当前线程指运行此方法的线程)

isInterrupted():测试线程是否已经中断

中断的理解:

通过中断并不能停止一个线程 需要被中断的线程自己去处理

被中断的线程如何知道自己被中断了?

线程对象有一个标识位来表示是否被中断了

测试:

package com.tony;

public class MyThread extends Thread {
	@Override
	public void run() {
		System.out.println("running...");
	}
}
public class ThreadTest {

	public static void main(String[] args) throws InterruptedException {
		MyThread myThread = new MyThread();
		myThread.start();
		
		Thread.sleep(5*1000); 
		
		myThread.interrupt(); // 设置中断(中断位为true)
		System.out.println("是否中断了interrupted():" + myThread.interrupted());
		System.out.println("是否中断了interrupted():" + myThread.interrupted());
		
		
	}

}

输出结果:

running...
是否中断了interrupted():false
是否中断了interrupted():false

MyTread实现类被设置了中断位 为什么输出都是false

interrupted()测试的是当前线程是否中断 main线程没有被中断 所以false

package com.tony;

public class MyThread extends Thread {

	@Override
	public void run() {

		System.out.println("是否中断了interrupted():" + this.interrupted());
		System.out.println("是否中断了interrupted():" + this.interrupted());

	}
}
package com.tony;

public class ThreadTest {

	public static void main(String[] args) {
		MyThread myThread = new MyThread();
		myThread.start();
		
		myThread.interrupt(); // 设置中断(中断为true)
		
	}

}

输出结果:

是否中断了interrupted():true
是否中断了interrupted():false

true:可以理解 当前线程是MyThread并被设置了中断位 

false:???



查看API文档一目了然

第一次调用时  先判断中断位在清除这个中断位 

没有中断位了 第二次调用时 判断是否已经中断返回false


package com.tony;

public class MyThread extends Thread {

	@Override
	public void run() {
		System.out.println("running...");
	}
}
package com.tony;
public class ThreadTest {
	public static void main(String[] args) throws InterruptedException {
		MyThread myThread = new MyThread();
		myThread.start();
		myThread.interrupt(); // 设置中断(中断为true)
		System.out.println("是否中断了isInterrupted():" + myThread.isInterrupted());
		System.out.println("是否中断了isInterrupted():" + myThread.isInterrupted());

	}
}

输出结果:

是否中断了isInterrupted():true
是否中断了isInterrupted():true
running...

使用例子:

package com.tony;

public class MyThread extends Thread {

	@Override
	public void run() {
		for (int i = 0; i < 8888888; i++) {

			if (this.isInterrupted()) {
				System.out.println("线程被中断了");
				return; // 线程退出
			} else {
				System.out.println("模拟线程正在干活...");
			}
		}
	}
}
package com.tony;

public class ThreadTest {

	public static void main(String[] args) throws InterruptedException {
		MyThread myThread = new MyThread();
		myThread.start();
		Thread.sleep(5 * 1000);
		myThread.interrupt();

	}

}

输出结果:

模拟线程正在干活...
模拟线程正在干活...
模拟线程正在干活...
......
模拟线程正在干活...
模拟线程正在干活...
模拟线程正在干活...
线程被中断了


测试在sleep中断线程:

package com.tony;

public class MyThread extends Thread {

	@Override
	public void run() {
		try {
			Thread.sleep(5*1000);			
		}catch(InterruptedException e) {
			System.out.println("sleep中被中断:" + this.isInterrupted());
			e.printStackTrace();
		}
	}
}
package com.tony;

public class ThreadTest {

	public static void main(String[] args) throws InterruptedException {
		MyThread myThread = new MyThread();
		myThread.start();
		Thread.sleep(2 * 1000);
		myThread.interrupt();

	}

}

输出结果:

sleep中被中断:false
java.lang.InterruptedException: sleep interrupted
	at java.lang.Thread.sleep(Native Method)
	at com.tony.MyThread.run(MyThread.java:8)

false?

sleep中被中断 中断位被清除 所以返回false


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值