Java Thread类的静态布尔型interrupted()方法(带示例)
线程类静态布尔interrupted()
软件包java.lang.Thread.interrupted()中提供了此方法。
此方法用于检查线程是否已被中断。
此方法是静态的,因此我们也可以使用类名访问此方法。
此方法的返回类型为boolean,因此如果线程已中断,则返回true,然后将boolean变量或interrupted标志设置为false后,否则返回false;如果线程未中断,则返回false。
此方法引发异常。
语法:
static boolean interrupted(){
}参数:
在Thread方法中,我们不传递任何对象作为参数。
返回值:
此方法的返回类型为boolean,返回true或false,如果线程已中断,则返回true,然后将boolean标志设置为false,否则返回false。
Java程序演示interrupted()方法示例
/* We will use Thread class methods so we are importing
the package but it is not mandate because
it is imported by default
*/
import java.lang.Thread;
class InterruptedThread extends Thread
{
//覆盖run()Thread类的方法
public void run()
{
for(int i=0;i<=3;++i){
/* By using interrupted() method to check whether
this thread has been interrupted or not it will
return and execute the interrupted code
*/
if(Thread.interrupted())
{
System.out.println("Is thread" +Thread.currentThread().getName()+"has been interrupted and status is set to "+" " +Thread.interrupted());
}
else
{
System.out.println("This thread has not been interrupted");
}
}
}
public static void main(String args[])
{
InterruptedThread it1 = new InterruptedThread();
InterruptedThread it2 = new InterruptedThread();
/* By using start() method to call the run() method of
Thread class and Thread class start() will call run()
method of InterruptedThread class
*/
it2.start();
it2.interrupt();
it1.start();
}
}输出结果
E:\Programs>javac InterruptedThread.java E:\Programs>java InterruptedThread This thread has not been interrupted This thread has not been interrupted This thread has not been interrupted Is thread Thread-1 has been interrupted: false This thread has not been interrupted This thread has not been interrupted This thread has not been interrupted This thread has not been interrupted
热门推荐
10 香港老妈结婚祝福语简短
11 毕业立体贺卡祝福语简短
12 简短新年年会祝福语
13 评论小品祝福语大全简短
14 恭喜师兄结婚祝福语简短
15 员工集体辞职祝福语简短
16 高中新生祝福语 简短
17 装修祝福语男生搞笑简短
18 生日开业蛋糕祝福语简短