如何在Android上顺序运行java线程

时间:2012-12-18 16:08:12

标签: java android

我有七个声纳传感器,我的目标是在Android平板电脑上依次显示这些传感器值。

为了在平板电脑屏幕上动态显示传感器值,我使用了Runnable s。

但是,如果声纳传感器并行工作,则会引起声纳传感器之间的干扰。因此,我需要让它们按顺序工作。

我的问题是:是否可以按顺序使用线程在Android平板电脑上显示传感器值?

虽然我使用了join()sleep(),但传感器仍在相互干扰。当我尝试使用wait和notify时,应用程序会给出内存不足的异常。我也尝试使用同步方法,但它并没有影响它。

下面你可以看到我的代码。

package com.dinning;

import android.widget.TextView;

public class Dinning implements Runnable 
{
    public Interfacekit diningkit;
    public TextView[] Rfidviews; 
    int count=1;

    public Dinning(Interfacekit diningkit,Rfid drfid1,Rfid drfid2, Rfid drfid3,Rfid drfid4,Rfid drfid5,Rfid drfid6,Rfid fsrfid1,Rfid fsrfid2,Rfid fsrfid3,final TextView[] Rfidviews)
    {
        this.diningkit = diningkit;
        this.Rfidviews = Rfidviews;
        new Thread(this).start();
    }

    public void run()       
    {
        while(count==1)
        {   
            if(diningkit.getattach()==true)
            {
                Thread t1 = new Thread(new Runnable() 
                {  
                    public void run() 
                    {
                        Rfidviews[11].post(new Runnable() 
                        {
                            public void run() 
                            {
                                {
                                    int sonar0 = diningkit.getsonar(7);
                                    if(sonar0!=0)
                                        Rfidviews[11].setText("Port0:"+sonar0);
                                    else
                                        Rfidviews[11].setText(" ");
                                }            
                            }
                        });
                    }
                });

                Thread t2 = new Thread(new Runnable() {  
                    public void run() {
                        Rfidviews[12].post(new Runnable() 
                        {
                            public void run() 
                            {
                                {
                                    int sonar1 = diningkit.getsonar(6);
                                    if(sonar1!=0)
                                        Rfidviews[12].setText("Port1:"+sonar1);
                                    else
                                        Rfidviews[12].setText(" ");
                                }            
                            }
                        });
                    }
                });

                Thread t3 = new Thread(new Runnable() 
                {  
                    public void run() 
                    {
                        Rfidviews[13].post(new Runnable() 
                        {
                            public void run() 
                            {
                                {
                                    int sonar2 = diningkit.getsonar(5);     
                                    if(sonar2!=0)
                                        fidviews[13].setText("Port2:"+sonar2);
                                    else
                                        Rfidviews[13].setText(" ");
                                }        
                            }
                        });
                    }       
                });

                t1.start();
                t2.start();
                t3.start();
            } 
        }//while        
    }           
}





 package com.dinning;



import android.app.Activity;
import android.content.Context;
import com.phidgets.InterfaceKitPhidget;
import com.phidgets.Phidget;
import com.phidgets.PhidgetException;
import com.phidgets.event.AttachEvent;
import com.phidgets.event.AttachListener;
import com.phidgets.event.DetachEvent;
import com.phidgets.event.DetachListener;
import com.phidgets.event.InputChangeEvent;
import com.phidgets.event.InputChangeListener;
import com.phidgets.event.SensorChangeEvent;
import com.phidgets.event.SensorChangeListener;

public class Interfacekit {

    int status=1;
    InterfaceKitPhidget ik;
    public boolean attach = false;
    public Phidget phidget;
    int value = 0;
    int index = 0;
    int serial;

    int value0,value1,value2,value3,value4,value5,value6,value7,valport;
    int sonarvalue0,sonarvalue1,sonarvalue2,sonarvalue3,sonarvalue4,sonarvalue5,sonarvalue6,sonarvalue7;
    int curVal;

    public Interfacekit(final Context con,final Activity act,int serial)
    {
        this.serial = serial;

        try
        {
            com.phidgets.usb.Manager.Initialize(con);
            ik = new InterfaceKitPhidget();



            ik.addAttachListener(new AttachListener() {
                public void attached(final AttachEvent ae)
                {
                    act.runOnUiThread(new Runnable() {

                        public void run() {
                            try{
                            attach=true;    

                            value0= ik.getSensorValue(0);
                            value1= ik.getSensorValue(1);
                            value2= ik.getSensorValue(2);
                            value3=ik.getSensorValue(3);
                            value4=ik.getSensorValue(4);
                            value5= ik.getSensorValue(5);
                            value6= ik.getSensorValue(6);
                            value7= ik.getSensorValue(7);


                            }catch(Exception e){}
                        }

                    });




                }
            });



            ik.addDetachListener(new DetachListener() {
                public void detached(final DetachEvent ae) {

                    attach = false;
                    phidget = ae.getSource();
                    act.runOnUiThread(new Runnable() {

                        public void run() {

                        }
                    });
                }
            });


            ik.addSensorChangeListener(new SensorChangeListener() {
                public void sensorChanged(SensorChangeEvent se) {  //final Sensorchangeevent


                    value = se.getValue();
                    index = se.getIndex();



                    try {

                        sonarvalue0=ik.getSensorValue(0);
                        sonarvalue1=ik.getSensorValue(1);
                        sonarvalue2=ik.getSensorValue(2);
                        sonarvalue3=ik.getSensorValue(3);
                        sonarvalue4=ik.getSensorValue(4);
                        sonarvalue5=ik.getSensorValue(5);
                        sonarvalue6=ik.getSensorValue(6);
                        sonarvalue7=ik.getSensorValue(7);





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









                }



            });




            ik.addInputChangeListener(new InputChangeListener() {
                public void inputChanged(final InputChangeEvent ie) {



                }
            });
            ik.open(serial);

        }
        catch(Exception e){}
    }



        public boolean getattach()
        {

         return attach ;    
        }

        /* Sonar*/

        public int getsonar(int digitalOutput){

            int curVal,result = 0;


            if(5==digitalOutput){



                    try {
                        ik.setOutputState(digitalOutput, true);

                    } 
                    catch (PhidgetException e) {

                    e.printStackTrace();
                    }



                    curVal=(int) (1.296*sonarvalue2);

                    if(curVal>0 && curVal<60){
                        result=curVal;
                        System.out.println("Port2: "+result);
                    }
                    else if (curVal>60)
                        result=0;



            }

            else if(6==digitalOutput){



                try {
                    ik.setOutputState(digitalOutput, true);

                } 
                catch (PhidgetException e) {

                e.printStackTrace();
                }



                curVal=(int) (1.296*sonarvalue1);

                if(curVal>0 && curVal<80)
                    result=curVal;

                else if(curVal>80) 
                    result=0;



            }


            else if(7==digitalOutput){



                try {
                    ik.setOutputState(digitalOutput, true);

                } 
                catch (PhidgetException e) {

                e.printStackTrace();
                }



                curVal=(int) (1.296*sonarvalue0);

                if(curVal>0 && curVal<50)
                    result=curVal;

                else if(curVal>50)
                    result=0;



            }

            return result;

    }





        /* End Sonar*/



}

2 个答案:

答案 0 :(得分:3)

在java.util.concurrent包中使用ExecutorService。特别是Executors.newSingleThreadExecutor();

  

<强> newSingleThreadExecutor

     

public static ExecutorService newSingleThreadExecutor()   创建一个Executor,它使用一个在无界队列中运行的工作线程。 (但请注意,如果由于在关闭之前执行期间的故障而导致此单个线程终止,则在执行后续任务时需要使用新线程。)保证任务顺序执行,否则在任何给定时间,多个任务都将处于活动状态。与其他等效的newFixedThreadPool(1)不同,保证返回的执行程序不可重新配置以使用其他线程。

另外

t1.start();
t1.join();
t2.start();

等。当另一个死亡时开始一个。但是我根本不认为需要线程。不必要的复杂化和它们的设计目的?

答案 1 :(得分:1)

而不是

Thread t1 = new Thread(new Runnable() {  
                public void run() {
                   //do something
                }
            });

在线程中运行run()方法。

尝试

(new Runnable() {  
    public void run() {
        //do something
    }
 }).run();

创建匿名Runnable并直接调用其run()(而不是通过Thread),因此会按顺序执行。

编辑0 : 你似乎有很多ThreadRunnable s没有明确的理由。我相信这段代码应该是等效的,但会简化许多ThreadRunnable,这使得它难以阅读:

package net.ukims.dsp.cid.dataaccess;

import android.widget.TextView;

public class Dinning implements Runnable {
    public Interfacekit diningkit;
    public TextView[] Rfidviews;
    int count = 1;

    public Dinning(Interfacekit diningkit, Rfid drfid1, Rfid drfid2,
            Rfid drfid3, Rfid drfid4, Rfid drfid5, Rfid drfid6, Rfid fsrfid1,
            Rfid fsrfid2, Rfid fsrfid3, final TextView[] Rfidviews) {
        this.diningkit = diningkit;
        this.Rfidviews = Rfidviews;
        run();
    }

    public void run() {
        while (count == 1) {
            if (diningkit.getattach() == true) {
                Rfidviews[11].post(new Runnable() {
                    public void run() {
                        {
                            int sonar0 = diningkit.getsonar(7);
                            if (sonar0 != 0)
                                Rfidviews[11].setText("Port0:" + sonar0);
                            else
                                Rfidviews[11].setText(" ");
                        }
                    }
                });

                Rfidviews[12].post(new Runnable() {
                    public void run() {
                        {
                            int sonar1 = diningkit.getsonar(6);
                            if (sonar1 != 0)
                                Rfidviews[12].setText("Port1:" + sonar1);
                            else
                                Rfidviews[12].setText(" ");
                        }
                    }
                });

                Rfidviews[13].post(new Runnable() {
                    public void run() {
                        {
                            int sonar2 = diningkit.getsonar(5);
                            if (sonar2 != 0)
                                fidviews[13].setText("Port2:" + sonar2);
                            else
                                Rfidviews[13].setText(" ");
                        }
                    }
                });             
            }
        }// while
    }
}