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

199
Vistas
El temporizador continúa después de la cantidad de tiempo especificada

Estoy tratando de realizar lo que hay dentro de la condición while, pero cuando la condición finaliza, los subprocesos continúan verificando la salida. Necesito detener lo que hay dentro del ciclo while de acuerdo con el temporizador, básicamente haciendo el trabajo del temporizador.

 public static long start = System.currentTimeMillis(); public static long end = start + 10 * 1000; // 10 seconds * 1000 ms/sec while (System.currentTimeMillis() <= end) { Thread t = new Thread(new UserGenerator()); Thread t1 = new Thread(new Vote()); t.start(); Thread.sleep(1000); t1.start(); Thread.sleep(1000); } System.err.println("Time Done");

producción

 Name:Alaine,Email:lg@shapirosher.com ,SSN:516517 Name:Adriana,Email:donbenchoff@comcast.net ,SSN:526527 Time Done User 516517 Voted User 526527 Voted BUILD SUCCESSFUL (total time: 16 seconds)
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Esto es lo que hace el código anterior:

  • Se ejecuta durante 10 segundos.
  • Genera un hilo de UserGenerator y un hilo de Vote por segundo
  • Entra en estado de suspensión en el medio

Entonces, después de 10 segundos, el ciclo while terminará y el hilo main saldrá. Sin embargo, 5 threds iniciados por él continuarán ejecutándose (hasta que finalice su método de run o se produzca una excepción). Depende de cómo run implemente el método de ejecución en las clases UserGenerator y Vote . Por ejemplo, si hay un ciclo infinito, esos subprocesos continuarán ejecutándose y el programa nunca se cerrará.

over 4 years ago · Santiago Trujillo Denunciar

0

No estoy seguro de tener suficiente información para una respuesta completa, sin embargo, puede intentar esto:

 long start = System.currentTimeMillis(); long end = start + (10 * 1000); // 10 seconds * 1000 ms/sec List<UserGenerator> us = new ArrayList<>(); List<Vote> vs = new ArrayList<>(); Thread t, t1; while (System.currentTimeMillis() <= end) { UserGenerator u; Vote v; t = new Thread( u = new UserGenerator()); t1 = new Thread(v = new Vote()); t.start(); us.add(u); if(end - System.currentTimeMillis() >= 1000) Thread.sleep(1000); t1.start(); vs.add(v); if(end - System.currentTimeMillis() >= 1000) Thread.sleep(1000); } t.interrupt(); t1.interrupt(); //the following may be redundant. It depends in what // UserGenerator and Vote do for(int i =0; i < us.ssize() ; i++) { us.get(i).stop(); vs.get(i).stop(); } System.err.println("Time Done"); } long start = System.currentTimeMillis(); long end = start + (10 * 1000); // 10 seconds * 1000 ms/sec List<UserGenerator> us = new ArrayList<>(); List<Vote> vs = new ArrayList<>(); Thread t, t1; while (System.currentTimeMillis() <= end) { UserGenerator u; Vote v; t = new Thread( u = new UserGenerator()); t1 = new Thread(v = new Vote()); t.start(); us.add(u); if(end - System.currentTimeMillis() >= 1000) Thread.sleep(1000); t1.start(); vs.add(v); if(end - System.currentTimeMillis() >= 1000) Thread.sleep(1000); } t.interrupt(); t1.interrupt(); //the following may be redundant. It depends in what // UserGenerator and Vote do for(int i =0; i < us.ssize() ; i++) { us.get(i).stop(); vs.get(i).stop(); } System.err.println("Time Done"); }
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