jfcmacro

Ejemplo de sincronización, ignorar las interrupciones

Mar 29th, 2019
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.66 KB | None | 0 0
  1. class EjemploHilo8 implements Runnable {
  2.  
  3.     public EjemploHilo8() {
  4.     }
  5.  
  6.     public void run() {
  7.     Thread t = Thread.currentThread();
  8.  
  9.     while (true) {
  10.         System.out.println("Esto es otro hilo: " + t);
  11.        
  12.         if (Thread.interrupted()) {
  13.         System.out.println("Ya voy");
  14.         }
  15.        
  16.     }
  17.     //System.out.println("Good bye, cruel world!");
  18.     }
  19.  
  20.     public static void main(String []args) {
  21.     ThreadGroup tg = new ThreadGroup("fumador");
  22.     Thread eh7_1 = new Thread(tg, new EjemploHilo8(), "Ejemplo hilo 8 1");
  23.  
  24.     eh7_1.start();
  25.     try {
  26.        
  27.         while (true) {
  28.         Thread.currentThread().sleep(2000);
  29.         eh7_1.interrupt();
  30.         }
  31.  
  32.     }
  33.     catch (InterruptedException ie) {
  34.     }
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment