Return to main () after using start () method

1

After running the command gerenciador.start() , my program does not return to the main function, and does not print the finished message. It simply performs the command I have spoken. Does anyone know why this happens?

  public class Principal 
{
    private static long startTime = System.currentTimeMillis();

    public static void main(String[] args) 
    {
        Fila sharedLine = new Fila();

        Elevador elev = new Elevador(sharedLine);
        Esquiador gerenciador = new Esquiador(sharedLine);

        gerenciador.start();
        //elev.start();
        System.out.println("acabou");
        long endTime = System.currentTimeMillis();
        System.out.println("It took " + (int)(((endTime - startTime)/1000)/60) + " milliseconds");
    }
}



public class Esquiador extends Thread 
{
     Fila sharedLine;
     static boolean todosEntraram = false;
    //String filaQuePertenco;
    //int idEsquiador;

    public Esquiador(Fila fila) 
    {
        super("Esquiador");
        sharedLine = fila;
        //filaQuePertenco = filaQ;
        //idEsquiador=id;

    }

    public void run()
    {

        for(int i = 0; i < 30; i++)
        {

            try 
             {
                System.out.println("O esquiador está dormindo.");
                Thread.sleep(500);
             } 

            catch (InterruptedException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            Esquiadores esquis = new Esquiadores(i);

            //System.out.println("O esquiador "+ esquis.idEsquiador+ " entrou em seu 'run'");
            //while(true)
            //{
                    //política do esquiador para decidir em qual fila ele irá entrar

                    //printando o tamanho de cada fila.
                    System.out.println("O esquiador "+esquis.idEsquiador+" chegou para ingressar numa fila.");
                    System.out.println("Tamanho LS"+sharedLine.elementosLS.size());
                    System.out.println("Tamanho RS"+sharedLine.elementosRS.size());
                    System.out.println("Tamanho LT"+sharedLine.elementosLT.size());
                    System.out.println("Tamanho RT"+sharedLine.elementosRT.size());
                    System.out.println("Esquiador operando ...");


                    if(sharedLine.elementosLS.size()<2*sharedLine.elementosLT.size() && sharedLine.elementosLS.size()<2*sharedLine.elementosRT.size() && sharedLine.elementosLS.size()<sharedLine.elementosRS.size())
                    {
                        //this.filaQuePertenco = "LS";
                        //System.out.println("add ls");
                        sharedLine.adicionaFilaLS(esquis);

                        //printando elementos da fila LS

                    }

                    else if(sharedLine.elementosRS.size()<2*sharedLine.elementosLT.size() && sharedLine.elementosRS.size()<2*sharedLine.elementosRT.size() && sharedLine.elementosRS.size()<=sharedLine.elementosLS.size())
                    {
                        //this.filaQuePertenco = "RS";
                        //System.out.println("add rs");
                        sharedLine.adicionaFilaRS(esquis);

                        //printando elementos da fila RS
                    }

                    else if(sharedLine.elementosLT.size()<=sharedLine.elementosRT.size())
                    {
                        //this.filaQuePertenco = "LT";
                        //System.out.println("add lt");
                        sharedLine.adicionaFilaLT(esquis);

                    }

                    else 
                    {
                        //this.filaQuePertenco = "RT";
                        //System.out.println("add rt");
                        sharedLine.adicionaFilaRT(esquis);
                    }

                    System.out.println("Situação das filas após a operação:");
                    System.out.println("Tamanho LS"+sharedLine.elementosLS.size());
                    System.out.println("Tamanho RS"+sharedLine.elementosRS.size());
                    System.out.println("Tamanho LT"+sharedLine.elementosLT.size());
                    System.out.println("Tamanho RT"+sharedLine.elementosRT.size());
            //}
        }
        Esquiador.todosEntraram = true;
    }
}
    
asked by anonymous 12.12.2016 / 00:59

1 answer

1

The execution is returning to the main method, but different than what you imagine. The "finished" line is printed before the first iteration of the Esquiador thread.

I think you were expecting the start method to execute the run method of the thread and only then return to main , however, the purpose of a thread is to execute in parallel at the same time.

In this case, as you force a pause at the beginning of for of the thread, this almost invariably causes the main method to finish before the first sleep .

To wait for the completion of a thread, use the join method, like this:

gerenciador.start();
gerenciador.join(); //somente continue depois que a thread finalizar

In addition to this misunderstanding, there are several other problems or at least potential problems:

  • The time display is dividing the time that is already in milliseconds by 1000 and by 60, so the result will be in hours, not longer.
  • Your threads appear to be sharing objects and running operations without synchronization and without protection mechanisms against concurrency issues. In your final implementation, you should make sure that every time you change an object, this occurs atomically.
  • It's a little difficult to understand the exact intent of the code, but if the original idea was to have 30 skiers, the implementation is completely wrong. You have to create 30 threads and not a thread containing a loop executed 30 times sequentially.
  • 12.12.2016 / 02:01