Android - 在这种多线程方法中迷失方向 - 将数据和线程执行传递给更高层次的对象

时间:2013-12-18 16:05:53

标签: java android multithreading parameter-passing hierarchy

我真的想不起,不知怎的,我几乎到了最后,但现在我希望,这不会是一条死胡同。

我想将数据从继承的线程对象传递回我的父对象。 并且还可以一次性返回主线程。

这是我的父对象

public class ControllerBase implements IController , IHandleRequestOnChange, IHandlesCustomTimer{ 
     private QueueWorkerThread _QueueWorker;        

     // some other vars and methods

     // ctor and so on .....


    // initializer 

    public void init(){ 
     // some other code
     _QueueWorker = new QueueWorker();
     _QueueWorker.SetEventListener(this);     // i want to announce this object from _QueueWorker
     _QueueWorker.start()                     // starts the thread

    // other initializations
    }

   @Override
   public void OnQueueWorkerReady(DataToPass){
      // from IHandleRequestOnChange

      // ALL INSIDE THIS CODE SHALL BE PROCESSED IN UI THREAD. BUT HOW ?
      DataReceived dataRec = new DataReceived();

      dataRec =  this.Request(DataToPAss);  
      this.ApplyDataToUIControls(dataRec);
   }

}

这是我的QueueWorkerThread:

public class QueueWorkerThread extends Thread implements IRequestQueueProcessed{

  // ctor
  public QueueWorkingThread(){
    super();        
  }

 // other variables and methods
  IHandlesRequestOnChange _Listener;

 public void Enque(DataToPass Data){
     this.add(Data);
 }

 public void SetEventListener( IHandlesRequestOnChange pEventListener) {
    this._Listener = pEventListener;
 @Override 
 public void run(){
    // here a LinkedBlockingQueue is polled
    // AND UNDER SOME CONDITIONS I WANT TO PASS THE POLLED DATA BACK TO 
    // THE PARENT OBJECT AND RETURN TO MAIN THREAD. HOW CAN I ACHIEVE THIS?

    if(AcertainConditionIsMet == true){
       // I want to return to UI thread
        this.OnQueueWorkerReady(the_polled_data)
    }

    // and this thread shall proceed and do some other stuff......
 }


@Override 
public void OnQueueWorkerReady(TableToPass Data){
 // of interface  IRequestQueueProcessed
 //  
 // calling parents callback via interface-casting
 ((IHandleRequestOnChange)_Listener).OnQueueWorkerReady(null, Data);    

 // this passes execution AND DATA to my parent object BUT I do not return to main thread

}



}

1 个答案:

答案 0 :(得分:0)

我认为你必须在应用程序对象中共享你的处理程序才能在处理程序的消息中传回数据。例如,你可以:

public class MyApplication extends Application {
    private Handler handler;
    public Handler getHandler(){ return handler;}
    public void setHandler(Handler handler){ this.handler = handler;}
    private static MyApplication instance;
    public static MyApplication getMyApplication(){return instance;}
    @Override
    public void onCreate() {
    super.onCreate();

    instance = this;
}

}

注意静态方法能够在没有上下文的情况下检索应用程序对象(如在线程中)。

我想如果你想更新你的UI,你应该在一些活动上下文中,所以在你的活动中,你声明了处理程序:

private Handler handler;

在onCreate()中,您可以在app对象中实例化并存储处理程序:

handler = new Handler(){
public void handleMessage(android.os.Message msg) {
    //In here you can do your UI update using the references to the views   
}
};
((MyApplication)getApplication()).setHandler(handler);

修改 您现在可以使用公共静态方法从线程中获取应用程序对象的实例。

希望它有所帮助。