0

I have an activity which, essentially, starts multiple threads in order to get multiple resources from network. I update the UI after receiving the notifications from these threads using ResultReceiver mechanism. The code snippet looks like below,

public void onReceiveResult(int resultCode, Bundle resultData){

    switch(resultCode){

    case 1: updateData1();
        break;

    case 2: updateData2();
        break;

    case 3: updateData3();
        break;
}

As, its the main thread which updates the UI elements on receiving the notifications from these threads, I was thinking it will be a good idea to make method onReceiveResult synchronized. Any comments? Is there is anything else that should be taken care of in order keep it efficient and safe?

4

1 回答 1

0

不,这无济于事,它会阻塞线程。而是在 UI 线程上运行将修改 UI 的代码,否则您将遇到问题,具体取决于调用此 onReceiveResult 的位置...

activity.runOnUiThread(new Runnable() {

           @Override
           public void run() {
               updateData1();
           }
});
于 2013-11-15T11:47:14.887 回答