日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

Java Thread类的静态布尔型interrupted()方法(带示例)

發(fā)布時間:2023/12/1 java 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java Thread类的静态布尔型interrupted()方法(带示例) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

線程類靜態(tài)布爾型interrupted() (Thread Class static boolean interrupted())

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

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

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

    此方法用于檢查線程是否已被中斷。

  • This method is static so we can access this method with the class name too.

    該方法是靜態(tài)的,因此我們也可以使用類名訪問此方法。

  • The return type of this method is boolean so it returns true if the thread has been interrupted and then after boolean variable or interrupted flag is set to false else returns false if the thread has not been interrupted.

    此方法的返回類型為boolean,因此如果線程已中斷,則返回true,然后在boolean變量或interrupted標(biāo)志設(shè)置為false之后,否則返回false,如果線程尚未中斷。

  • This method raises an exception.

    此方法引發(fā)異常。

Syntax:

句法:

static boolean interrupted(){}

Parameter(s):

參數(shù):

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

我們不會在Thread方法中將任何對象作為參數(shù)傳遞。

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 then set a boolean flag to false else return false.

此方法的返回類型為boolean ,它返回true或false;如果線程已中斷,則返回true,然后將boolean標(biāo)志設(shè)置為false,否則返回false。

Java程序演示interrupted()方法的示例 (Java program to demonstrate example of interrupted() 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 classpublic 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(); } }

Output

輸出量

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

翻譯自: https://www.includehelp.com/java/thread-class-static-boolean-interrupted-method-with-example.aspx

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的Java Thread类的静态布尔型interrupted()方法(带示例)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。