how to wait for Android runOnUiThread to be finished?

后端 未结 7 1876
予麋鹿
予麋鹿 2020-12-29 00:58

I have a worker thread that creates a runnable object and calls runOnUiThread on it, because it deals with Views and controls. I\'d like to use the result of the work of the

相关标签:
7条回答
  • 2020-12-29 01:33

    I think the simplest way to achieve this is using a "CountDownLatch".

    final CountDownLatch latch = new CountDownLatch(1);
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
    
            // Do something on the UI thread
    
            latch.countDown();
        }
    });
    try {
        latch.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    
    // Now do something on the original thread
    

    (I believe this question is a duplicate of "How to make timer task to wait till runOnUiThread completed")

    0 讨论(0)
  • 2020-12-29 01:35

    Perhaps a little simplistic but a mutex will do the job:

    final Semaphore mutex = new Semaphore(0);
    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            // YOUR CODE HERE
            mutex.release();
        }
    });
    
    try {
        mutex.acquire();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    
    0 讨论(0)
  • 2020-12-29 01:36

    Andrew answer is good, I create a class for easier use.

    Interface implementation :

    /**
     * Events for blocking runnable executing on UI thread
     * 
     * @author 
     *
     */
    public interface BlockingOnUIRunnableListener
    {
    
        /**
         * Code to execute on UI thread
         */
        public void onRunOnUIThread();
    }
    

    Class implementation :

    /**
     * Blocking Runnable executing on UI thread
     * 
     * @author 
     *
     */
    public class BlockingOnUIRunnable
    {
        // Activity
        private Activity activity;
    
        // Event Listener
        private BlockingOnUIRunnableListener listener;
    
        // UI runnable
        private Runnable uiRunnable;
    
    
        /**
         * Class initialization
         * @param activity Activity
         * @param listener Event listener
         */
        public BlockingOnUIRunnable( Activity activity, BlockingOnUIRunnableListener listener )
        {
            this.activity = activity;
            this.listener = listener;
    
            uiRunnable = new Runnable()
            {
                public void run()
                {
                    // Execute custom code
                    if ( BlockingOnUIRunnable.this.listener != null ) BlockingOnUIRunnable.this.listener.onRunOnUIThread();
    
                    synchronized ( this )
                    {
                        this.notify();
                    }
                }
            };
        }
    
    
        /**
         * Start runnable on UI thread and wait until finished
         */
        public void startOnUiAndWait()
        {
            synchronized ( uiRunnable )
            {
                // Execute code on UI thread
                activity.runOnUiThread( uiRunnable );
    
                // Wait until runnable finished
                try
                {
                    uiRunnable.wait();
                }
                catch ( InterruptedException e )
                {
                    e.printStackTrace();
                }
            }
        }
    
    }
    

    Using it :

    // Execute an action from non-gui thread
    BlockingOnUIRunnable actionRunnable = new BlockingOnUIRunnable( yourActivity, new BlockingOnUIRunnableListener()
    {
        public void onRunOnUIThread()
        {
            // Execute your activity code here
        }
    } );
    
    actionRunnable.startOnUiAndWait();
    
    0 讨论(0)
  • 2020-12-29 01:38

    A solution might be to leverage Java's FutureTask<T> which has the benefit that someone else has already dealt with all the potential concurrency issues for you:

    public void sample(Activity activity) throws ExecutionException, InterruptedException {
        Callable<Void> callable = new Callable<Void>() {
            @Override
            public Void call() throws Exception {
                // Your task here
                return null;
            }
        };
    
        FutureTask<Void> task = new FutureTask<>(callable);
        activity.runOnUiThread(task);
        task.get(); // Blocks
    }
    

    You can even return a result from the main thread by replacing Void with something else.

    0 讨论(0)
  • 2020-12-29 01:44

    Use the AsyncTask class, its methods onPostExecure and onProgressUpdate are executed by the MAIN thread (the UI thread).

    http://developer.android.com/reference/android/os/AsyncTask.html

    Is the better way for your case!

    Hope this can help you.

    0 讨论(0)
  • 2020-12-29 01:47

    In case someone faces this while developing in a Xamarin app I leave here my C# code that made the trick.

    My RecyclerView adapter was being set too late so it was returning null in my ScrollListener constructor. This way my code waits for the ui thread to finish the work and release the "lock".

    All this is running inside a Fragment (Activity returns the parent activity object).

    Semaphore semaphore = new Semaphore(1, 1);
    semaphore.WaitOne();
    Activity?.RunOnUiThread(() =>
    {
        leaderboard.SetAdapter(adapter);
        semaphore.Release();
    });
    semaphore.WaitOne();
    scrollListener = new LazyLoadScrollListener(this, (LinearLayoutManager)layoutManager);
    leaderboard.SetOnScrollListener(scrollListener);
    semaphore.Release();
    

    Hope it helps somebody.

    0 讨论(0)
提交回复
热议问题