Functions do not end

1

After executing the 4 functions it should print the time it took to execute

public class main 
{
public static void main(String[] args)
{
    long init  = System.currentTimeMillis(); 
    ataque1();
    ataque2();
    ataque3();
    ataque4();
    long end  = System.currentTimeMillis(); 
    long diff = end - init;
    System.out.print("Demorou " + (diff / 1000) + " segundos");
}

static int for1 = 0;
static int for2 = 20 / 4;
static int for3 = for2 + for2;
static int for4 = for2 = for3;

public static void ataque1()
{
    new Thread() 
    { 
        @Override 
        public void run() 
        {
            int for11 = for1;
            int for22 = for2;
            int for33 = for3;
            int for44 = for4;

            for(int i = for1 ; i <= for2; i++)
            {
                /*if(i == busca)
                {
                    break;
                }*/

                System.out.println(i + " de 999999"); 
            }
        }
    }.start();
}
public static void ataque2()
{
    new Thread() 
    { 
        @Override 
        public void run() 
        {
            int for11 = for1;
            int for22 = for2;
            int for33 = for3;
            int for44 = for4;

            for(int i = for2; i <= for3; i++)
            {
                /*if(i == busca)
                {
                    break;
                }*/

                System.out.println(i + " de 999999"); 
            }
        }
    }.start();

}
public static void ataque3()
{
    new Thread() 
    { 
        @Override 
        public void run() 
        {
            int for11 = for1;
            int for22 = for2;
            int for33 = for3;
            int for44 = for4;

            for(int i = for3; i <= for4; i++)
            {
                /*if(i == busca)
                {
                    break;
                }*/

                System.out.println(i + " de 999999"); 
            }
        }
    }.start();
}
public static void ataque4()
{
    new Thread() 
    { 
        @Override 
        public void run() 
        {
            int for11 = for1;
            int for22 = for2;
            int for33 = for3;
            int for44 = for4;
            for(int i = for4; i <= 20; i++)
            {
                /*if(i == busca)
                {
                    break;
                }*/

                System.out.println(i + " de 999999"); 
            }
        }
    }.start();
}

But the time it takes to run in the middle of the run rather than the end is

0 de 999999
1 de 999999
2 de 999999
3 de 999999
4 de 999999
5 de 999999
6 de 999999
7 de 999999
8 de 999999
9 de 999999
Demorou 0 segundos10 de 999999
11 de 999999
12 de 999999
13 de 999999
14 de 999999
15 de 999999
16 de 999999
17 de 999999
18 de 999999
19 de 999999
20 de 999999
    
asked by anonymous 06.07.2015 / 23:49

2 answers

4

Your program is starting 4 threads, and is not expecting them to finish running before printing time - which is exactly what it is doing. If you really want to calculate the time, you have to wait for the threads to finish, for example, by calling the Thread.join .

public static void main(String[] args)
{
    long init  = System.currentTimeMillis(); 
    Thread t1 = ataque1();
    Thread t2 = ataque2();
    Thread t3 = ataque3();
    Thread t4 = ataque4();
    t1.join();
    t2.join();
    t3.join();
    t4.join();
    long end  = System.currentTimeMillis(); 
    long diff = end - init;
    System.out.print("Demorou " + (diff / 1000) + " segundos");
}

public static Thread ataque1()
{
    Thread t = new Thread()
    {
        // implementação
    };
    t.start();
    return t;
}
// O mesmo para ataque[2-4]
    
06.07.2015 / 23:53
2

A more elegant solution would be to use ThreadGroup .

Here is an example:

//adicionado Grupo
static ThreadGroup tg; 

public static void main(String[] args) {
   // instancia de grupo de threads - ataque
    tg = new ThreadGroup("ataque");

    long init = System.currentTimeMillis();
    ataque1();
    ataque2();
    ataque3();
    ataque4();
    long end = System.currentTimeMillis();
    long diff = end - init;

    // obtem threads ativas do grupo
    Thread[] thds = new Thread[tg.activeCount()];
    int nthds = tg.enumerate(thds);
    // aguarda todas do grupo para continuar
    for (int i = 0; i < nthds; i++) {
            thds[i].join();
    }

    System.out.print("Demorou " + (diff / 1000) + " segundos");
}

public static void ataque1() {
    // declaração de grupo e nome da thread
    new Thread(tg, "ataque1") {
    // prossegue até ataque4 ...
    
07.07.2015 / 00:41