如何在此特定线程上使用.join()与线程

时间:2015-05-29 12:05:08

标签: java android multithreading synchronized

我的应用程序正在以非常高的速度进行一些繁重的算法计算。每次更改方向传感器时,如果算法尚未运行,则激活算法。 该算法为Threaded,因为无论算法是否正在运行,我都希望继续使用我的应用程序ButtonsExit按钮。如果我在method上调用算法trackingActivity,则会激活算法但我无法在我的应用屏幕上使用任何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

}

3 个答案:

答案 0 :(得分:1)

如果要在线程中按顺序调用方法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();

答案 1 :(得分:0)

假设您为线程创建了一个类

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

你可以通过在循环时添加行myThread.join()作为最后一行来等待这个线程完成。

这将导致主线程在继续之前等待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();
}

答案 2 :(得分:0)

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();
}