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: falseHere, 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()之间的区别的全部內容,希望文章能夠幫你解決所遇到的問題。