Los metodos interrupt que se manejan en Java. Son 3 Interrupt() Interrupted() isInterrupted()
Interrupcionces Sintaxis Public void interrupt() Interrumpe este thread
Primero el metodo checkAccess de este thread es invocado, lo cual puede causar una Security Exception.
Si este thread es bloqueado al invocar el wait(), wait(long), o wait(long, int) metodos de la clase objeto, o de los metodos join(), join(long), join(long,int), sleep(long), o sleep(long, int) de esta clase entonces los mismos interrupts(de la misma clase) seran despejados y se recivira un Interrupted Exception.
Si este thread es bloqueado en una operacion de E/S ante un canal interrumpible, entonces el canal se cerrrara, el estado de interrupt del thread se fijara, y el thread recivira un Closed by Interrupt Exception?.
Si este thread es bloqueado en un selector, entonces el estado del thread de intrrupt sera fijado, y retornara inmediatamente de la operacion de seleccion, posiblemente con un valor diferente a cero, justo como si el metodo del selector wakeup hubiera sido invocado.
Si ninguna de las condiciones pasadas se cumplen, entonces el estado en interrupt del thread sera fijado (activado).
Throws: Secutity Exception?. Si el thread actual no puede cambiar el thread.
Interrupted (interrumpido)
Sintaxis Public static boolean interrupted()
Hace una prueba si el thread actual a sido interrumpido. El estado de interrupcion del thread es deshecho por este metodo. En otras palabras, si este metodo se llamara dos veces, la segunda vez devolveria un valor falso (al menos que el thread actual sea interrumpido de nuevo, despues de que la primera llamada haya terminado su interrupcion y antes de que la segunda empiece).
Returns: Verdadero si el thread actual a sido interrumpido, falso de lo contrario
isInterrupted
Sintaxis Public boolean isInterrupted() Hace una pruebaa para ver si este thread a sido interrumpido. El estado de interrupcion del thread no es afectado por este metodo.
Codigo Java
import java.applet.*; import java.io.*;
public class File IO? extends Applet {
private static final int ACTION_GET_FILE_LENGTH = 0;
private String filename = null;
private String fileLength = “-1″;
private int action = −1;
private Thread jsThread = null;
private Thread mt = null;
public void init() {
//final Object applet = this;
// have to use a Thread because an “access denied” exception occurs if the
// read file originates from Java Script (even if the applet is signed).
Runnable r = new Runnable() {
public void run() {
//synchronized(applet) {
while (true) {
try {
Thread.sleep(Integer.MAX_VALUE);//applet.wait();
} catch (Interrupted Exception e) {}
try {
if (action == ACTION_GET_FILE_LENGTH) {
File f = new File(filename);
if (f.exists())
fileLength = String.valueOf(f.length());
else
fileLength = “-1″;
}
} catch (Throwable err) {
err.printStackTrace();
}
try {
jsThread.interrupt(); // always called
} catch (Throwable err) {}
}
//}
}
};
mt = new Thread®;
mt.start();
}
public synchronized String getFileLength(String filename) {
try {
this.filename = filename;
action = ACTION_GET_FILE_LENGTH;
jsThread = Thread.currentThread();
mt.interrupt();
Thread.sleep(Integer.MAX_VALUE);
} catch (Interrupted Exception e) {}
return fileLength;
}
} Integrantes del Equipo Gonzalez Vazques David Antonio Valenzuela Casanova Jacobo Javier import java.applet.*; import java.io.*;
public class File IO extends Applet {
private static final int ACTION_GET_FILE_LENGTH = 0;
private String filename = null;
private String fileLength = “-1″;
private int action = −1;
private Thread jsThread = null;
private Thread mt = null;
public void init() {
//final Object applet = this;
// have to use a Thread because an “access denied” exception
occurs if the
// read file originates from Java Script (even if the applet
is signed).
Runnable r = new Runnable() {
public void run() {
//synchronized(applet) {
while (true) {
try {
Thread.sleep(Integer.MAX_VALUE);//applet.wait();
} catch (Interrupted Exception e) {}
try {
if (action ==
ACTION_GET_FILE_LENGTH) {
File f = new File(filename);
if (f.exists())
fileLength =
String.valueOf(f.length());
else
fileLength = “-1″;
}
} catch (Throwable err) {
err.printStackTrace();
}
try {
jsThread.interrupt(); // always
called
} catch (Throwable err) {}
}
//}
}
};
mt = new Thread®;
mt.start();
}
public synchronized String getFileLength(String filename) {
try {
this.filename = filename;
action = ACTION_GET_FILE_LENGTH;
jsThread = Thread.currentThread();
mt.interrupt();
Thread.sleep(Integer.MAX_VALUE);
} catch (Interrupted Exception e) {}
return fileLength;
}
}