interrumpido () y isInterrupted () en Java : Aquí, vamos a aprender lo que son los diferencias entre el interrumpida () y isInterrupted () en el lenguaje de programación Java?
interrumpido () y isInterrupted () en Java
Aquí, vamos a ver cómo () difiere de isInterrupted interrumpida () en Java?
isInterrupted ()
- Este método está disponible en java.lang class.
- Este es el método no class por lo que este método es accesible con el objeto package.
- Este método se utiliza para comprobar si un hilo se ha interrumpido o no interrumpido.
- El tipo static de este método es class por lo que devuelve verdadero si el hilo se ha interrumpido falsa return cosa.
- En boolean del método isInterrupted () , tenemos que notar que este método devuelve true si el mensaje ha sido interrumpido y luego después de la interrupción no establece de nuevo la variable return tan falsa como como método interrumpido () devuelve demás falso.
- La sintaxis de este método se da a continuación:
public boolean isInterrupted(){
}
Ejemplo:
import java.lang.Thread;
class InterruptedThread extends Thread {
// Overrides run() method of Thread class
public void run() {
for (int i = 0; i <= 3; ++i) {
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();
it2.start();
it2.interrupt();
it1.start();
}
}
salida
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
Aquí, vamos a ver forma interrumpida () difiere de isInterrupted () en Java?
interrumpido ()
- Este método está disponible en java.lang case.
- Este es un método boolean lo que este método se puede acceder con el nombre class también.
- Este método se utiliza para comprobar si un hilo se ha interrumpido o no interrumpido y luego ajustar el estado de la bandera interrumpido.
- El tipo package de este método es static por lo que devuelve verdadero si el hilo se ha interrumpido falsa class cosa.
- En return del método interrumpido () , hay que notar que este método devuelve verdadero si el mensaje ha sido interrumpida y luego, después de la bandera de interrupción o variable boolean se establece en false en caso retornos falsos.
- La sintaxis de este método se da a continuación:
public static boolean interrupted(){
}
Ejemplo:
import java.lang.Thread;
class InterruptedThread extends Thread {
// Overrides run() method of Thread class
public void run() {
for (int i = 0; i <= 3; ++i) {
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();
it2.start();
it2.interrupt();
it1.start();
}
}
salida
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