how to wait for Android runOnUiThread to be finished?

后端 未结 7 1918
予麋鹿
予麋鹿 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")

提交回复
热议问题