Interupcion de hilo

Páginas: 6 (1345 palabras) Publicado: 9 de diciembre de 2010
Interrupción del hilo en Java In our overview of thread methods , we saw various methods that throw InterruptedException . En nuestra visión general de los métodos de hilo , vimos varios métodos que lanzan InterruptedException.
Interruption is a mechanism whereby a thread that is waiting (or sleeping) can be made to prematurely stop waiting . La interrupción es un mecanismo mediante el cual unhilo que se espera (o dormir) se puede hacer para parar antes de tiempo de espera.
Incidentally, it is important not to confuse thread interruption with either software interrupts (where the CPU automatically interrupts the current instruction flow in order to call a registered piece of code periodically— as in fact happens to drive the thread scheduler ) and hardware interrupts (where the CPUautomatically performs a similar task in response to some hardware signal). Dicho sea de paso, es importante no confundir la interrupción de hilos, ya sea con interrupciones de software (donde la CPU automáticamente interrumpe el flujo actual de la instrucción con el fin de llamar a un pedazo de código registradas periódicamente-como de hecho sucede para conducir el programador de subprocesos ) ylas interrupciones de hardware (donde la CPU automáticamente realiza una tarea similar en respuesta a alguna señal de hardware).
To illustrate interruption, let's consider again a thread that prints a message periodically. Para ilustrar la interrupción, vamos a considerar de nuevo un tema que imprime un mensaje de forma periódica. After printing the message, it sleeps for a couple of seconds,then repeats the loop: Después de imprimir el mensaje, se detiene por un par de segundos, luego se repite el bucle:
Runnable r = new Runnable() { r = Ejecutable Ejecutable nuevo () {
public void run() { public void run () {
try { try {
while (true) { while (true) {
Thread.sleep(2000L); Thread.Sleep (2000L);
System.out.println("Hello, world!"); System.out.println ("Hola, mundo!");
} }} catch (InterruptionException iex) { } Catch (iex InterruptionException) {
System.err.println("Message printer interrupted"); System.err.println ("Mensaje de la impresora interrumpida");
} }
} }
}; };
Thread thr = new Thread(r); THR hilo = new Thread (r);
thr.start(); thr.start ();

The InterruptedException is thrown by the Thread.sleep() method, and in fact by a few other core librarymethods that can "block", principally: El InterruptedException se produce por la Thread.Sleep () método, y de hecho por unos pocos básicos colección de otros métodos que pueden "bloquear", principalmente:
Object.wait() , part of the wait/notify mechanism; Object.wait (), que forma parte de la espera / notify mecanismo;
Thread.join() , that makes the current thread wait for anotherthread to complete; Thread.Join (), que hace que la espera hilo actual de otro hilo para completar;
Proess.waitFor() , which lets us wait for an external process (started from our Java application) to terminate; Proess.waitFor (), que nos permite esperar a que un proceso externo (iniciado desde nuestra aplicación Java) para terminar;
various methods in the Java 5 concurrency libraries, suchas the tryLock() method of the Java 5 ReentrantLock class. varios métodos en la concurrencia cinco bibliotecas Java, como la tryLock () el método de Java 5 ReentrantLock clase.
In general, InterruptedException is thrown when another thread interrupts the thread calling the blocking method. En general, InterruptedException se produce cuando otro hilo interrumpe el hilo de una llamada al método debloqueo. The other thread interrupts the blocking/sleeping thread by calling interrupt() on it: El otro hilo interrumpe el dormitorio de hilos, bloqueo de llamadas de interrupción () en él:
thr.interrupt(); thr.interrupt ();
Provided that the thread or task calling sleep() (or whatever) has been implemented properly, the interruption mechanism can therefore be used as a way to cancel tasks ....
Leer documento completo

Regístrate para leer el documento completo.

Estos documentos también te pueden resultar útiles

  • hilo
  • Hilos
  • hila
  • hilos
  • Hila
  • Hilos
  • HILO
  • hila

Conviértase en miembro formal de Buenas Tareas

INSCRÍBETE - ES GRATIS