jfcmacro

Ejemplo de suicidio de hilo

Mar 28th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.45 KB | None | 0 0
  1. class HiloSeppuku implements Runnable {
  2.  
  3.     public void run() {
  4.     Thread t = Thread.currentThread();
  5.  
  6.     while(true) {
  7.         System.out.println("Hilo: " + t);
  8.         if (t.isInterrupted()) break; // Seppuku done!
  9.     }
  10.     }
  11.  
  12.     public static void main(String args[]) {
  13.     Thread t = new Thread(new HiloSeppuku());
  14.  
  15.     t.start();
  16.  
  17.     try {
  18.         Thread.sleep(4000);
  19.         t.interrupt();
  20.         Thread.sleep(3000);
  21.         t.join();
  22.     }
  23.     catch (InterruptedException ie) {
  24.     }
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment