如何使计时器任务等到runOnUiThread完成

时间:2012-01-04 19:43:55

标签: java android multithreading timertask ui-thread

我需要在一段时间后更新UI,为此我创建了一个计时器计划,在其中我调用了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");

我遇到的问题是,在达到“3”后,定时器线程跳转到“8”,之后UI线程从“4”运行。 我想让定时器线程等到UI线程在“7”完成它的工作,然后它应该移动到“8”。

示例输出

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

2 个答案:

答案 0 :(得分:3)

试试这个

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

答案 1 :(得分:1)

我认为实现这一目标的最简单方法是使用" 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