Clase Hilo 1

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package threads1; /** * * @author office */ public class Ejercicio1 { public static void main(String[] args) { //Hilo principal Thread thread = Thread.currentThread(); System.out.println("El hilo principal se llama: " + thread.getName()); //Cambiando de nombre thread.setName("Hilo principal"); System.out.println("El hilo principal se llama: " + thread.getName()); try { System.out.println("Hola "); Thread.sleep(1000); System.out.println("desde "); Thread.sleep(1000); System.out.println("Java..."); Thread.sleep(1000); } catch (InterruptedException e) { } //utilizando nuestro hilo custom CustomThread th = new CustomThread("Custom"); //utilizando nuestro hilo second SecondThread th2 = new SecondThread("Segundo"); SecondThread th3 = new SecondThread("Tercero"); SecondThread th4 = new SecondThread("Cuarto"); //Teniendo control sobre la clase custom System.out.println("con join.."); CustomThread th5 = new CustomThread("Quinto"); CustomThread th6 = new CustomThread("sexto"); CustomThread th7 = new CustomThread("septimo"); System.out.println("Esta vivo=" + th5.isAlive()); try { th5.join(); th6.join(); th7.join(); } catch (InterruptedException e) { } System.out.println("Esta vivo=" + th5.isAlive()); System.out.println("Terminado"); } } class CustomThread extends Thread { CustomThread(String nombre) { //constructor de inico estandar super(nombre); start(); } @Override public void run() { System.out.println("Ejecutando hilo " + Thread.currentThread().getName()); try { System.out.println("[Hola ]"); sleep(1000); System.out.println("[desde ]"); sleep(1000); System.out.println("[Java ...]"); sleep(1000); } catch (InterruptedException e) { } } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.