How to make timer task to wait till runOnUiThread completed

十年热恋 提交于 2020-01-15 06:22:12

问题


I need to update UI after certain time period, for which I have create a timer schedule and inside it I am calling the runOnUiThread.

timer.scheduleAtFixedRate(new TimerTask() {

            public void run() {
                System.out.println("1");
                    try {

                    System.out.println("2");
                    System.out.println("3");

                    runOnUiThread(new  Runnable() {

                        public void run() {
                            System.out.println("4");
                            System.out.println("5");
                            System.out.println("6");
                            System.out.println("7");
                        }
                    });

                    System.out.println("8");
                } catch (Exception e) {

                    e.printStackTrace();
                }

            }

        }, delay, period);
        System.out.println("9");

I am having the problem that after reaching "3", timer thread jumps to "8" and after that the UI thread runs from "4". I want to make timer thread to wait till UI thread completes its work at "7" only then it should move to "8".

Sample Output

01-05 00:30:16.308: I/System.out(1394): 1
01-05 00:30:16.308: I/System.out(1394): 2
01-05 00:30:16.308: I/System.out(1394): 3
01-05 00:30:16.308: I/System.out(1394): 8
01-05 00:30:16.308: I/System.out(1394): 4
01-05 00:30:16.308: I/System.out(1394): 5
01-05 00:30:16.308: I/System.out(1394): 6
01-05 00:30:16.308: I/System.out(1394): 7
01-05 00:30:17.307: I/System.out(1394): 1
01-05 00:30:17.307: I/System.out(1394): 2
01-05 00:30:17.307: I/System.out(1394): 3
01-05 00:30:17.307: I/System.out(1394): 8
01-05 00:30:17.307: I/System.out(1394): 4
01-05 00:30:17.323: I/System.out(1394): 5
01-05 00:30:17.323: I/System.out(1394): 6
01-05 00:30:17.323: I/System.out(1394): 7

回答1:


Try this

Object lock=new Object();
timer.scheduleAtFixedRate(new TimerTask() {

        public void run() {
            System.out.println("1");
                try {

                System.out.println("2");
                System.out.println("3");

                runOnUiThread(new  Runnable() {

                    public void run() {
                        System.out.println("4");
                        System.out.println("5");
                        System.out.println("6");
                        System.out.println("7");
                        synchronized(lock){lock.notify();}
                    }
                });
                try{
                   synchronized(lock){lock.wait();}
                }catch(InterruptedException x){}
                System.out.println("8");
            } catch (Exception e) {

                e.printStackTrace();
            }

        }

    }, delay, period);
    System.out.println("9");



回答2:


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


来源:https://stackoverflow.com/questions/8732987/how-to-make-timer-task-to-wait-till-runonuithread-completed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!