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

148
Vistas
Executing some code only once in a Thread region

In a certain part of a Java code that i am working, i need to place a timer inside a run() method. Each thread will execute all code inside run(). But i need to start measuring after block (1) and before block of code (2) so the timer needs to be triggered there.

for (int i = 0; i < total_threads-1; i++){
   final int id = i+1;          
   th[i] = new Thread(new Runnable() {              
       public final void run(){
             /* ... block of code (1) executed by multiple threads ... */

             /* How can i start this counter only once? */
             final long begin = System.currentTimeMillis();

             /* ... another block of code (2) executed by multiple threads i need to measure!!! ... */
       }
    });
    th[i].start();
}

for(int i = 0 ; i < total_threads-1 ; i++) {
    try {
           th[i].join();
        }
    catch (InterruptedException e) {}
}

final long end = System.currentTimeMillis();

System.out.println((end-begin) / 1000.0);

But all the threads will have their own begin variable and start the counter which is a problem because System.currentTimeMillis() should be triggered once and not by many threads. I probably could separate the code of run() in two different parallel regions but would imply creating the threads twice which would be unacceptable (in terms of performance). There is a similar technique of OpenMP directive #pragma omp master for Java using Java threads? How can i measure the time correctly here?

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

0

You can check the thread ID to execute prefer line once:

if (id == YOUR_THREAD_ID) 
{
     begin = System.currentTimeMillis();
}
over 4 years ago · Santiago Trujillo Denunciar

0

The simplest way would just be to record the time in the master thread (i.e. the thread which creates all the children) instead of in the children themselves.

But if you really want to start the timer in the children (maybe there's some expensive setup?), you could use a Guava StopWatch object.

The code would look something like:

StopWatch timer = StopWatch.createUnstarted();
for (int i = 0; i < total_threads-1; i++){
    final int id = i+1;
    th[i] = new Thread(() -> {
        /* ... block of code (1) executed by multiple threads ... */

        try {
            synchronized (timer) {
                timer.start();
            }
        } catch (IllegalStateException e) {
            // ignore; the watch is already started
        }

        /* ... another block of code (2) executed by multiple threads i need to measure!!! ... */
    });
    th[i].start();
}

for(int i = 0 ; i < total_threads-1 ; i++) {
    try {
        th[i].join();
    } catch (InterruptedException e) {}
}

timer.stop();

System.out.println(timer.elapsed(TimeUnit.SECONDS));
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