将runnable发布到UI线程直到完成后调用wait() [英] Calling wait() after posting a runnable to UI thread until completion

查看:209
本文介绍了将runnable发布到UI线程直到完成后调用wait()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序线程可以继续之前,我实际上需要等待ui线程执行runnable。 wait()/ notify()方式是一种正确的方法,还是有更好的方法呢?我实际上做的是这样的:

I'm actually in need of waiting for the ui thread to execute a runnable before my application thread can continue. Is the wait()/notify() way a proper way to do it or is there something better for this? What I'm actually doing looks like this:

public void showVideoView() {
    try {
        final AtomicBoolean done = new AtomicBoolean(false);
        final Runnable task = new Runnable() {
            @Override
            public void run() {
                synchronized(this) {
                    mStartupCurtain.setVisibility(View.GONE);
                    mVideoView.setVisibility(View.VISIBLE);
                    mWebView.loadUrl("about:blank");
                    mWebView.setVisibility(View.GONE);
                    done.set(true);
                    notify();
                }
            }
        };
        mUiHandler.post(task);
        synchronized(task) {
            while(!done.get()) {
                task.wait();
            }
            Log.d(TAG, "showVideoView done!");

        }
    } catch (InterruptedException e) {
        Log.e(TAG, "Thread got interrupted while waiting for posted runnable to finish its task");
    }

}

当我这样做时,我有确保线程不是UI之一,当我从一个来自MediaPlayer.OnCompletionListener之类的接口的侦听器方法调用方法时会发生这种情况。

Also when I do this I have to be sure that the thread is not the one of the UI, which happens when I start calling methods from a listener method coming from an interface like MediaPlayer.OnCompletionListener.

你觉得呢?

推荐答案

对我来说没问题。

done变量可以是常规布尔值而不是AtomicBoolean,因为您最终在锁定内获取/设置它的值。我喜欢你在调用wait之前检查done的值 - 因为在你在worker线程中输入锁之前,任务很可能已经完成。如果你还没有这样做,那么wait()调用将无限期地发生,因为notify()已经发生了。

The "done" variable could be a regular Boolean instead of AtomicBoolean since you definitively get/set it's value within the lock. I like that you check the value of "done" prior to calling wait - since it is quite possible the task will have been completed before you ever enter the lock in the worker thread. If you had not done that, the wait() call would go indefinitely since the notify() had already happened.

有一个边缘情况要考虑可能或可能不适用于您的设计。当工作线程仍在等待任务完成时,如果UI线程试图退出(即app退出)会发生什么?另一种变化是工作线程在等待任务完成时,但UI线程正在等待工作线程退出。后者可以用另一个布尔变量来解决,UI线程通过该布尔变量通知工作线程退出。这些问题可能相关也可能不相关 - 取决于UI如何管理线程开始。

There is one edge case to consider that may or may not be applicable to your design. What happens if the UI thread is attempting to exit (i.e. app exit) when the worker thread is still stuck waiting for the task to complete? Another variation is when the worker thread is waiting on the task to complete, but the UI thread is waiting on the worker thread to exit. The latter could be solved with another Boolean variable by which the UI thread signals the worker thread to exit. These issues may or may not be relevant - depending on how the UI is managing the thread to begin with.

这篇关于将runnable发布到UI线程直到完成后调用wait()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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