如何在此特定线程中将 .join() 与线程一起使用

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

我的应用程序正在以非常高的速度进行一些繁重的算法计算。每次更改方向传感器时,如果尚未 运行,它就会激活算法。 算法是 Threaded 因为我想继续使用我的应用程序 ButtonsExit 按钮,无论算法是否是 运行。如果我在 trackingActivity 上调用算法 method,算法将被激活,但我无法在我的应用程序屏幕上使用任何 button。线程在这种情况下很有用,尤其是因为我需要在应用程序主屏幕上显示算法结果。这就是为什么我需要使用 Threading- 使算法成为并行过程。 我使用 .join() 的主要建议是因为我不希望传感器更改中断来激活我的算法,只要当前算法执行已经 运行.

这是我的代码 & Thread:

 package com.application.i;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class trackingActivity extends Activity implements SensorEventListener {
    TextView Xlocation;
    TextView Ylocation;
    TextView Zlocation;
    TextView Iterations;
    TextView Pit;
    TextView pitch;
    TextView roll;
    TextView yaw;
    private double azimuth_angle;
    private double roll_angle;
    private double pitch_angle;
    float[] orientations;

    private SensorManager mSensorManager;
    private Sensor mOrientation;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.trackingsub);

        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mOrientation = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
    }

    @Override
    protected void onResume() {
        mSensorManager.registerListener(this, mOrientation,
                SensorManager.SENSOR_DELAY_NORMAL);
        super.onResume();
        trackingActivity t = new trackingActivity();

        double[] Locations = t.Algo(pitch_angle, roll_angle, azimuth_angle);
    }

    public void onSensorChanged(SensorEvent event) {
        azimuth_angle = event.values[0];
        pitch_angle = event.values[1];
        roll_angle = event.values[2];
        // Do something with these orientation angles.
        double[] orientations = { pitch_angle, roll_angle, azimuth_angle };

        // mSensorManager.unregisterListener(this);
        pitch = (TextView) findViewById(R.id.pitcher);
        roll = (TextView) findViewById(R.id.roller);
        yaw = (TextView) findViewById(R.id.yawer);
        String p = String.valueOf(pitch_angle);
        String r = String.valueOf(roll_angle);
        String a = String.valueOf(azimuth_angle);
        pitch.setText(p);
        roll.setText(r);
        yaw.setText(a);



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

                     public void run()
                           {


                              try 
                               {
                        synchronized (this) { 
                             X = 10.6569;
                             Y = 20.3265;
                             Z = 30.259;

                                locations = algo(X,Y,Z,pitch_angle, roll_angle,
                                            azimuth_angle);

                             }

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




                           }
                            }).start();
    }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub
    }
}

我注意到 algo 方法执行时没有顺序,我的意思是这个 Thread 不等待它之前的动作并开始新的 Thread.

网上搜索发现.join().join()强制Thread等到完成,然后才重新开始。

我的问题是: 如何强制这个 Thread 只有在它完成之前的激活后才开始? 或者,这里如何实现这个.join()

解决方案:

 while(true){
      Thread t = 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();
                                } 




                           }
                            });
t.start();

t.join();  \ auto-correction my force you to use try-catch

}

假设您已经为您的话题创建了一个 class,例如

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

您可以通过添加行 myThread.join() 作为 while 循环的最后一行来等待此线程完成。

这将导致主线程在继续之前等待 myThread 完成。

因此您的代码将如下所示:

    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();
}
while(true){
    Thread toExec = 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();
                        } 




                   }
                    }).start();
                toExec.join();
}

如果要在线程中顺序调用方法algo()

    new Thread(new Runnable() {
        public void run() {
            while (true) {
                try {
                    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();
                }
            }
        }
    }).start();