工人线程到工人线程通信

时间:2018-03-20 19:34:24

标签: java android multithreading ipc handler

Java / Android -

我无法找到任何工作线程到工作线程的通信示例。这些例子总是涉及到UiThread。

我有一个HandlerThread可以获得补充工作,HandlerThread将工作分散到多个工作线程上。我希望多个工作线程能够与HandlerThread进行通信。

我所能做的就是让工作线程通过Handler与UiThread进行通信。谢谢!

1 个答案:

答案 0 :(得分:2)

public class ModelFragment extends Fragment implements Handler.Callback {

   Handler backHandler1, backHandler2, mainHandler;

   @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);

        HandlerThread backThread1 = new HandlerThread("BACK_THREAD_1");
        backThread1.start();
        backHandler1 = new Handler(backThread1.getLooper(), this);

        HandlerThread backThread2 = new HandlerThread("BACK_THREAD_2");
        backThread2.start();
        backHandler2 = new Handler(backThread2.getLooper(), this);

        mainHandler = new Handler(Looper.getMainLooper(), this);
        // sending message from main thread to thread 1
        backHandler1.obtainMessage(BACK1_WHAT, backObj).sendToTarget();
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch(msg.what){
            case BACK1_WHAT:
            // this code runs on thread 1

            // sending message to thread 2 from thread 1
            backHandler2.obtainMessage(BACK2_WHAT, backObj1).sendToTarget();
            return true;

            case BACK2_WHAT:
            // this code runs on thread 2

            // sending message to thread 1 from thread 2
            backHandler1.obtainMessage(BACK1_WHAT, backObj2).sendToTarget();

            // sending message to main thread from thread 2
            mainHandler.obtainMessage(MAIN_WHAT, backObj2).sendToTarget();
            return true;

            case MAIN_WHAT:
            // runs on ui thread
            return true;
        }
        return false;
    }

    @Override
    public void onDestroy() {
       if(null != backHandler1) {
           backHandler1.removeCallbacksAndMessages(null);
           if(null != backHandler1.getLooper())
               backHandler1.getLooper().quit();
       }
       if(null != backHandler2) {
           backHandler2.removeCallbacksAndMessages(null);
           if(null != backHandler2.getLooper())
               backHandler2.getLooper().quit();
       }
       super.onDestroy();
   }
}