Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class NuevoHilo extends Thread {
- public NuevoHilo() {
- // crea un nuevo hilo
- super("Hilo de prueba"); // llama al constructor de la clase Thread y le pasa el nombre identificador para el thre
- start(); // Inicia la propia instancia de este hilo
- }
- // Igualmente se inicia el run como si fuera un thread.
- public synchronized void run() {
- try {
- for(int i = 0; i< 5; i++) {
- System.out.println("Hilo de ejecuccion : " + i);
- // Dormimos el hilo 500 segundos
- Thread.sleep(500);
- }
- } catch (InterruptedException e) { // capturamos las excepcion interfaces
- System.out.println("Hilo interrumpido por una excepcion.");
- }
- System.out.println("El NuevoHilo ya termino.");
- }
- }
- class ExtendThread {
- public static void main(String args[]) {
- new NuevoHilo(); // se crea la instancia de la clase recien hecha
- try {
- for(int i = 0; i < 5; i++) {
- System.out.println("Main Thread: " + i);
- Thread.sleep(1000);
- }
- } catch (InterruptedException e) {
- System.out.println("Main thread interrupted.");
- }
- System.out.println("Main thread exiting.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement