The functions do not end

Asked

Viewed 40 times

1

After the execution of 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 that took in the middle of the execution appears instead of the end

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
  • What this line is doing? static int for4 = for2 = for3; You want the value of for4 be equal to for2 and equal to for3, which in turn is 10?

2 answers

4


Your program is starting 4 threads, and is not waiting for them to finish their run before printing out the time - which is exactly what it is doing. If you really want to calculate the time, you need to wait for the threads to finish, for example by calling the function 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]
  • It is not possible to give Join this way. what should I do ??

  • +1 but you need to use the join in the Thread in itself and not in the function as this code is doing. I suggest that each function returns the Thread created, and this is stored in a variable.

  • @correct mgibsonbr, the Join needs to be given in Thread and not in function. I edited my reply, thank you!

  • It worked fine, but you could tell me what the ". Join()" does exactly ??

  • When you call join in a class object Thread, the execution of your thread stops until that thread finishes its execution. Please see the documentation at http://docs.oracle.com/javase/6/docs/api/java/lang/Thread.html#Join() for more information.

  • But even all threads with "Join" they will run in parallel right ??

  • Yes. The call to thread.start() causes the thread to start its execution, and they continue running (concurrently) until they finish.

Show 2 more comments

2

A more elegant solution would be using Threadgroup.

Following 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 ...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.