everblut

Hilo herencia

Sep 20th, 2011
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. public class NuevoHilo extends Thread {
  2.  
  3. public NuevoHilo() {
  4. // crea un nuevo hilo
  5. super("Hilo de prueba"); // llama al constructor de la clase Thread y le pasa el nombre identificador para el thre
  6. start(); // Inicia la propia instancia de este hilo
  7. }
  8.  
  9. // Igualmente se inicia el run como si fuera un thread.
  10. public synchronized void run() {
  11. try {
  12. for(int i = 0; i< 5; i++) {
  13. System.out.println("Hilo de ejecuccion : " + i);
  14. // Dormimos el hilo 500 segundos
  15. Thread.sleep(500);
  16. }
  17. } catch (InterruptedException e) { // capturamos las excepcion interfaces
  18. System.out.println("Hilo interrumpido por una excepcion.");
  19. }
  20. System.out.println("El NuevoHilo ya termino.");
  21. }
  22. }
  23.  
  24. class ExtendThread {
  25. public static void main(String args[]) {
  26. new NuevoHilo(); // se crea la instancia de la clase recien hecha
  27. try {
  28. for(int i = 0; i < 5; i++) {
  29. System.out.println("Main Thread: " + i);
  30. Thread.sleep(1000);
  31. }
  32. } catch (InterruptedException e) {
  33. System.out.println("Main thread interrupted.");
  34. }
  35. System.out.println("Main thread exiting.");
  36. }
  37. }
Add Comment
Please, Sign In to add comment