Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

384
Vistas
¿Cómo llegar al primer hilo-0 Java?

Estaba haciendo mi taller y la pregunta aparece de la siguiente manera:

hola al revés

Escriba un programa llamado ReverseHello.java que cree un subproceso (llamémoslo Subproceso 1).

El subproceso 1 crea otro subproceso (Subproceso 2); El subproceso 2 crea el subproceso 3; y así sucesivamente, hasta el Hilo 50.

Cada subproceso debe imprimir "¡Hola desde el subproceso!", Pero debe estructurar su programa de manera que los subprocesos impriman sus saludos en orden inverso.

mi código es así:

 public class Task2 implements Runnable { static int threadNo = 1; @Override public void run() { if (threadNo <= 50) { Thread reverse = new Thread(new Task2()); reverse.setName("Thread "+ threadNo); threadNo++; reverse.start(); try { reverse.join(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("hello from " + Thread.currentThread().getName()); } public static void main(String[] args) throws InterruptedException { Thread t2 = new Thread(new Task2()); t2.start(); } }

la salida es así:

 hello from Thread 50 hello from Thread 49 hello from Thread 48 hello from Thread 47 hello from Thread 46 hello from Thread 45 hello from Thread 44 hello from Thread 43 hello from Thread 42 hello from Thread 41 hello from Thread 40 hello from Thread 39 hello from Thread 38 hello from Thread 37 hello from Thread 36 hello from Thread 35 hello from Thread 34 hello from Thread 33 hello from Thread 32 hello from Thread 31 hello from Thread 30 hello from Thread 29 hello from Thread 28 hello from Thread 27 hello from Thread 26 hello from Thread 25 hello from Thread 24 hello from Thread 23 hello from Thread 22 hello from Thread 21 hello from Thread 20 hello from Thread 19 hello from Thread 18 hello from Thread 17 hello from Thread 16 hello from Thread 15 hello from Thread 14 hello from Thread 13 hello from Thread 12 hello from Thread 11 hello from Thread 10 hello from Thread 9 hello from Thread 8 hello from Thread 7 hello from Thread 6 hello from Thread 5 hello from Thread 4 hello from Thread 3 hello from Thread 2 hello from Thread 1 hello from Thread-0

No pude averiguar de dónde viene este último hello from Thread-0 y cómo deshacerme de este, ya que la pregunta es solo del hilo 1 al 50.

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

El primer subproceso que inicia desde su método main nunca obtiene un nombre de subproceso establecido, por lo que tendrá un nombre predeterminado asignado por la JVM, que resulta ser "Thread-0" .

Una solución simple es establecer el nombre del subproceso al comienzo del método de run en el subproceso actual, en lugar de en el subproceso reverse iniciado:

 @Override public void run() { Thread.currentThread().setName("Thread "+ threadNo); if (threadNo <= 50) {
over 4 years ago · Santiago Trujillo Denunciar

0

Este código debería resolver tu problema.

 public class Task implements Runnable { private final int threadNo; public Task(int threadNo) { this.threadNo = threadNo; } @Override public void run() { if (threadNo <= 50) { Thread reverse = new Thread(new Task(threadNo + 1)); reverse.setName("Thread " + threadNo); reverse.start(); try { reverse.join(); } catch (InterruptedException e) { e.printStackTrace(); } } if (threadNo > 1) { System.out.println("hello from " + Thread.currentThread().getName()); } } public static void main(String[] args) { Thread t2 = new Thread(new Task(1)); t2.start(); } }
over 4 years ago · Santiago Trujillo Denunciar

0

Thread-0 es el nombre del subproceso inicial creado e iniciado desde el método main sin incrementar el contador de subprocesos en Task2 .

Entonces, para solucionar este problema, es suficiente comenzar el hilo inicial como:

 new Thread(new Task2(), "Thread "+ Task2.threadNo++).start();
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda