how to wait for Android runOnUiThread to be finished?
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.
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();
}
}
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();
}
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();
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.
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")
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.
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()
}
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.
精彩评论