[Solved] How to use .join() with Thread at this particular Thread


Assume you’ve created a class for your Thread like

Thread myThread = new Thread(new Runnable(...));
myThread.start();

you can wait for this thread to finish by adding the line myThread.join() as last line of yout while loop.

This will cause the main-thread to wait for myThread to finish before it continues.

So your code would look as follows:

    while(true){
        Thread myThread = new Thread(new Runnable() {

                 public void run()
                       {


                          try 
                           {
                    synchronized (this) { 
                         X = (double) Float.parseFloat(Splited2[textIndex]);
                         Y = (double) Float.parseFloat(Splited2[textIndex+1]);
                         Z = (double) Float.parseFloat(Splited2[textIndex+2]);
                         textIndex+=3;
                            locations = algo(X,Y,Z,pitch_angle, roll_angle,
                                        azimuth_angle);

                         }

                           } 
                           catch (Exception e) 
                           {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } 
                       }
                        });
myThread.start();
myThread.join();
}

1

solved How to use .join() with Thread at this particular Thread