Java Thread类的boolean isInterrupted()方法(带示例)

线程类boolean isInterrupted() (Thread Class boolean isInterrupted())

  • This method is available in package java.lang.Thread.isInterrupted().

    软件包java.lang.Thread.isInterrupted()中提供了此方法。

  • This method is used to check the thread, whether a thread has been interrupted or not.

    此方法用于检查线程是否已被中断。

  • This method is not static so we cannot access this method with a class name too.

    此方法不是静态的,因此我们也无法使用类名访问此方法。

  • The return type of this method is boolean so it returns true if the thread has been interrupted and else returns false if the thread has not been interrupted.

    此方法的返回类型为boolean,因此如果线程已中断,则返回true;否则,如果线程未中断,则返回false。

  • We need to remember that this method returns true if the thread has been interrupted and then does not set the flag to false like as interrupted() method.

    我们需要记住,如果线程已被中断,则此方法返回true,然后不像interrupted()方法那样将标志设置为false。

  • This method raises an exception.

    此方法引发异常。

Syntax:

句法:

    boolean isInterrupted(){
    }

Parameter(s):

参数:

We don't pass any object as a parameter in the method of the Thread.

我们不会在Thread方法中将任何对象作为参数传递。

Return value:

返回值:

The return type of this method is boolean, it returns true or false and if the thread has been interrupted so it returns true and else returns false.

此方法的返回类型为boolean ,返回true或false,并且如果线程已中断,则返回true,否则返回false。

Java程序演示isInterrupted()方法的示例 (Java program to demonstrate example of isInterrupted() method)

/*  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 {
 // Overrides run()  method of Thread class
 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.currentThread().isInterrupted()) {
    System.out.println("Is the thread " + Thread.currentThread().getName() + " has been interrupted:" + " " + Thread.currentThread().isInterrupted());
   } else {
    System.out.println(("Is the thread " + Thread.currentThread().getName() + " has been interrupted: " + " " + Thread.currentThread().isInterrupted()));
   }
  }
 }
 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();
 }
}

Output

输出量

E:\Programs>javac InterruptedThread.java
E:\Programs>java InterruptedThread
Is the thread Thread-1 has been interrupted: true
Is the thread Thread-0 has been interrupted: false
Is the thread Thread-1 has been interrupted: true
Is the thread Thread-1 has been interrupted: true
Is the thread Thread-0 has been interrupted: false
Is the thread Thread-1 has been interrupted: true
Is the thread Thread-0 has been interrupted: false
Is the thread Thread-0 has been interrupted: false


翻译自: https://www.includehelp.com/java/thread-class-boolean-isinterrupted-method-with-example.aspx

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值