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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java中interrupted()和isInterrupted()之间的区别

發布時間:2023/12/1 java 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java中interrupted()和isInterrupted()之间的区别 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Java中的interrupted()和isInterrupted() (interrupted() and isInterrupted() in Java)

Here, we will see how isInterrupted() differs from interrupted() in Java?

在這里,我們將看到isInterrupted()與Java中的interrupted()有何不同?

isInterrupted() (isInterrupted())

  • This method is available in java.lang package.

    此方法在java.lang包中可用。

  • This is the non-static method so this method is accessible with the class object.

    這是非靜態方法,因此可通過類對象訪問此方法。

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

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

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

    此方法的返回類型為boolean,因此如果線程已中斷,則返回true,否則返回false。

  • In case of isInterrupted() method, we need to notice that this method returns true if the thread has been interrupted and then after interrupting it does not again set the?boolean variable as false like as interrupted() method else returns false.

    對于isInterrupted()方法 ,我們需要注意的是,如果線程已被中斷,則此方法返回true,然后在中斷之后不再將布爾變量設置為false,就像interrupted()方法一樣,否則返回false。

  • The syntax of this method is given below:

    該方法的語法如下:

    public boolean isInterrupted(){ }

Example:

例:

/* 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.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 threadThread-1 has been interrupted: true Is the threadThread-0 has been interrupted: false Is the threadThread-1 has been interrupted: true Is the threadThread-1 has been interrupted: true Is the threadThread-0 has been interrupted: false Is the threadThread-1 has been interrupted: true Is the threadThread-0 has been interrupted: false Is the threadThread-0 has been interrupted: false

Here, we will see how interrupted() differs from isInterrupted() in Java?

在這里,我們將看到interrupted()與Java中的isInterrupted()有何不同?

.minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}

打斷() (interrupted())

  • This method is available in java.lang package.

    此方法在java.lang包中可用。

  • This is a static method so this method is accessible with the class name too.

    這是一個靜態方法,因此也可以使用類名訪問此方法。

  • This method is used to check whether a thread has been interrupted or not interrupted and then set interrupted flag status.

    此方法用于檢查線程是否已被中斷,然后設置中斷標志狀態。

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

    此方法的返回類型為boolean,因此如果線程已中斷,則返回true,否則返回false。

  • In case of the interrupted() method, we need to notice that this method returns true if the thread has been interrupted and then after interrupted flag or boolean variable is set to false else returns false.

    對于interrupted()方法 ,我們需要注意的是,如果線程已被中斷,則此方法返回true,然后在interrupted標志或布爾變量設置為false之后,否則返回false。

  • The syntax of this method is given below:

    該方法的語法如下:

    public static boolean interrupted(){ }

Example:

例:

/* 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/differences-between-the-interrupted-and-isinterrupted-in-java.aspx

總結

以上是生活随笔為你收集整理的Java中interrupted()和isInterrupted()之间的区别的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。