0

为什么后台线程会产生自己的 Handler & Looper 只是为了修改 UI 的组件。我知道简单来说:

  • Looper:在消息队列中循环执行任务

  • 处理程序:将任务发布到队列

看看我从互联网上的文章中获取的这个片段

public class MyActivityV2 extends Activity {

   private Handler mUiHandler = new Handler();
   private MyWorkerThread mWorkerThread;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      mWorkerThread = new MyWorkerThread("myWorkerThread");
      Runnable task = new Runnable() {
         @Override
         public void run() {
            for (int i = 0; i < 4; i++) {
               try {
                  TimeUnit.SECONDS.sleep(2);
               } catch (InterruptedException e) {
                  e.printStackTrace();
               }
               if (i == 2) {
                  mUiHandler.post(new Runnable() {
                     @Override
                     public void run() {
                        Toast.makeText(MyActivityV2.this,
                            "I am at the middle of background task",
                            Toast.LENGTH_LONG)
                            .show();
                     }
                  });
               }
            }
            mUiHandler.post(new Runnable() {
               @Override
               public void run() {
                  Toast.makeText(MyActivityV2.this,
                      "Background task is completed",
                      Toast.LENGTH_LONG)
                      .show();
               }
            });
         }
      };

      // MyWorkerThread == HandlerThread
      mWorkerThread.start();
      mWorkerThread.prepareHandler();
      // 2 starting of thread
      mWorkerThread.postTask(task);
      mWorkerThread.postTask(task);
   }

   @Override
   protected void onDestroy() {
      mWorkerThread.quit();
      super.onDestroy();
   }
}

//MyWorkerThread.java
 class MyWorkerThread extends HandlerThread {

   private Handler mWorkerHandler;

   public MyWorkerThread(String name) {
      super(name);
   }

   public void postTask(Runnable task){
      mWorkerHandler.post(task);
   }

   public void prepareHandler(){
      mWorkerHandler = new Handler(getLooper());
   }
}

要么是我完全误解了代码,要么是 Android 缺乏线程基础。所以我很抱歉。

后台线程基本上重复自己(两次)。主要思想是通过后台线程操作 UI 组件。

看看这个:

mWorkerHandler

为什么后台线程会创建自己的处理程序,如果它是操作 UI 组件的问题,为什么不只是引用 UI 线程处理程序并通过处理程序发布可运行。

 mWorkerHandler = new Handler(getLooper());

which is creating its own looper (background thread's looper), which indicates that the background thread creating its own Message Queue. Shouldn't it be supposed to just play around with the message queue of main thread and not background thread.

Thanks in advance.

4

1 回答 1

0

I can't vouch for a random article on the Internet, but this code is using Handlers correctly.

This line creates a handler that runs code on the UI thread:

private Handler mUiHandler = new Handler();

This method creates a handler that runs code in the background thread:

public void prepareHandler(){
  mWorkerHandler = new Handler(getLooper());
}

These lines post the Runnable to the background thread:

mWorkerThread.postTask(task);
mWorkerThread.postTask(task);

So in a nutshell, a background thread would use a looper and handler for the same reason the UI thread uses them: so code on other threads can post messages and runnables to it.

于 2016-09-07T12:36:13.330 回答