how to wait for Android runOnUiThread to be finished?

JavaAndroidMultithreading

Java Problem Overview


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 runnable object right away. How do I wait for it to finish? It doesn't bother me if it's blocking.

Java Solutions


Solution 1 - Java

Just scratching out the highlights

synchronized( myRunnable ) {
   activity.runOnUiThread(myRunnable) ;

   myRunnable.wait() ; // unlocks myRunable while waiting
}

Meanwhile... in myRunnable...

void run()
{
   // do stuff

   synchronized(this)
   {
      this.notify();
   }
}

Solution 2 - Java

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();
}

Solution 3 - Java

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();

Solution 4 - Java

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.

Solution 5 - Java

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")

Solution 6 - Java

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.

Solution 7 - Java

This is how I do with Kotlin Extension

Add Extension function to check Thread and run on UI thread

 fun Context.executeOnUIThreadSync(task: FutureTask<Boolean>) {
        if (Looper.myLooper() == Looper.getMainLooper()) {
            task.run()
        } else {
            Handler(this.mainLooper).post {
                task.run()
            }
        }
    }

It will block UI thread and wait for return

  fun checkViewShown(): Boolean {
        val callable: Callable<Boolean> = Callable<Boolean> {
            // Do your task in UI thread here
            myView != null && myView?.parent != null
        }
        val task: FutureTask<Boolean> = FutureTask(callable)
        applicationContext.executeOnUIThreadSync(task)
        return task.get()
    }


  

Solution 8 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondjcouchycouchView Question on Stackoverflow
Solution 1 - JavaAndrewView Answer on Stackoverflow
Solution 2 - JavaDDDView Answer on Stackoverflow
Solution 3 - Javaolivier_sdgView Answer on Stackoverflow
Solution 4 - JavaahaView Answer on Stackoverflow
Solution 5 - JavachetboxView Answer on Stackoverflow
Solution 6 - JavaSergi MascaróView Answer on Stackoverflow
Solution 7 - JavaHitesh SahuView Answer on Stackoverflow
Solution 8 - Javapedr0View Answer on Stackoverflow