【问题标题】:Communication between UI thread and other threads using handlerUI线程和其他线程之间使用处理程序进行通信
【发布时间】:2017-02-27 00:02:29
【问题描述】:

UI线程和后台线程如何进行线程间通信?我想在这里使用通用处理程序概念来更新我的 UI。 我的概念如下

new Thread(new Runnable() {
         public void run() {
             while (mProgressStatus < 100) {
                 mProgressStatus = doWork();

                 // Update the progress bar
                 mHandler.post(new Runnable() {
                     public void run() {
                         mProgress.setProgress(mProgressStatus);
                     }
                 });
             }
         }
     }).start();

我想使用两个类,一个类包含主线程,另一个类包含后台线程,它们使用相同的处理程序。我该如何实施? 我知道这很常见,但我发现很难准确实施。

【问题讨论】:

    标签: java android multithreading runnable httpconnection


    【解决方案1】:

    如果您不想使用静态概念,则可以从参数中传递任何内容。在下面的代码中,我实现了两个类。正如你所问的,我在两个线程类中都使用了公共处理程序。我将处理程序 h1 作为 Runnable 对象和 start() 方法的参数传递给那里以触发另一个线程类的 run() 方法。包含 run() 方法的线程是 UI(主)线程。我们必须使用 UI 线程来更新 UI。工作(后台)线程无法进行 UI 更新。 Worker 与 UI 之间的通信是通过处理程序完成的。所以,我在 UI 线程类中定义了处理程序 h2。当从后台线程类调用 UI 线程类构造函数时,我从来自构造函数的 h1 中获取我的 h2 值。我使用 h2 进行通信。实际上,h2和h1在系统中属于同一个内存空间。

    我做了下面两个类,做了线程通信供大家参考。

    头等舱

     public class MainActivity extends AppCompatActivity {
        Handler h1;
        Thread t;
        EditText editText;
        private Bundle bb = new Bundle();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            editText = (EditText) findViewById(R.id.editText);
    
            h1 = new Handler(Looper.getMainLooper()) {
    
                @Override
                public void handleMessage(Message msg) {
                    bb = msg.getData();
                    String str = bb.getString("udd");
                    editText.setText(str);
                    System.out.println(str);
                }
            };
            t = new Thread(new MyRunnable(h1)); //I pass Runnable object in thread so that the code inside the run() method
            //of Runnable object gets executed when I start my thread here. But the code executes in new thread
            t.start(); //thread started
    
            try {
                t.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
    
        }
    
    }
    

    二等

    public class MyRunnable implements Runnable {
        private Handler h2;
        public MyRunnable(Handler h) {
            this.h2 = h;
        }
    
        @Override
        public void run() {
    
            //everything inside rum method executes in new thread
            for(int i=0;i<10;i++) {
                Message m = Message.obtain(); //get null message
                Bundle b = new Bundle();
                b.putString("udd", "daju");
                m.setData(b);
                //use the handler to send message
                h2.sendMessage(m);
    
            }
        }
    }
    

    注意:当 thread.start() 发生时,它会触发 Runnable 类的运行,它会创建一个单独的线程。因此,每次调用 start() 时,都会有一个与被调用线程具有相同优先级的新线程。

    希望,这对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-27
      • 1970-01-01
      • 2011-04-13
      • 2021-07-16
      相关资源
      最近更新 更多