该UI使用Handler.Post修改如何通知工作线程()? [英] How to notify worker thread that UI is modified using Handler.Post()?

查看:149
本文介绍了该UI使用Handler.Post修改如何通知工作线程()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个工作线程偶尔我用更新发送到UI线程 Handler.Post()。在某些情况下,我需要工作线程等待,直到 Handler.Post()在<关于UI线程视图进行修改和执行/ STRONG> UI线程被修改,通知工作线程去...
这里是我的简单的工作线程:

 的WorkerThread =新主题(){
    @覆盖
    公共无效的run(){
        progressBarHandler.post(新的Runnable(){
            公共无效的run(){
                //第一步:这工程确定
                ActionModeButton.performClick();
            }
        }        //第二步:返回空指针异常,因为ActionMode
        //尚未创建和R.id.select_recording是
        // ActionMode按钮,如果我把视频下载(1000);这里
        //将正常工作。
        最后查看selectRecording = getActivity()
                .findViewById(R.id.select_recording);
        selectRecording.post(新的Runnable(){
            公共无效的run(){
                selectRecording.performClick();
            }
        });
    }
}
workerThread.start();


解决方案

使用synchronized块以等待和通知

 最后的处理程序处理程序=新的处理程序();
    最终目标锁定=新的对象();
    新主题(新的Runnable接口(){
        布尔完成= FALSE;
        @覆盖
        公共无效的run(){            handler.post(新的Runnable(){
                @覆盖
                公共无效的run(){
                    同步(锁){
                        //请在UI线程一些东西                        完成=真;
                        lock.notifyAll();
                    }
                }
            });            同步(锁){
                尝试{
                  如果(!完成)
                    lock.wait();
                }
                赶上(InterruptedException的E){                }
            }        }
    })。开始();

I have a worker thread and occasionally i send updates to the UI Thread using Handler.Post(). In some cases i need worker thread to wait until Handler.Post() executed on UI Thread and the view is modified and after UI thread is modified, notify the worker Thread to go on... here is my simple worker thread:

workerThread = new Thread() {
    @Override
    public void run() {
        progressBarHandler.post(new Runnable() {
            public void run() {
                //Step1: which works ok
                ActionModeButton.performClick();
            }
        }

        //Step2: returns null pointer exception because ActionMode
        //is not yet created and R.id.select_recording is an
        //ActionMode button if I put Thread.sleep(1000); here it
        //will work fine.
        final View selectRecording = getActivity()
                .findViewById(R.id.select_recording);
        selectRecording.post(new Runnable() {
            public void run() {
                selectRecording.performClick();
            }
        });
    }
}
workerThread.start();

解决方案

using synchronized block with wait and notify

final Handler handler = new Handler();
    final Object lock = new Object();
    new Thread(new Runnable() {
        boolean completed = false;
        @Override
        public void run() {

            handler.post(new Runnable() {
                @Override
                public void run() {
                    synchronized (lock) {
                        //Do some stuff on ui thread 

                        completed = true;
                        lock.notifyAll();                            
                    }
                }
            });

            synchronized (lock) {
                try {
                  if(!completed)
                    lock.wait();
                }
                catch (InterruptedException e) {

                }
            }

        }
    }).start();

这篇关于该UI使用Handler.Post修改如何通知工作线程()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆