2

我需要在特定时间段后更新 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
4

2 回答 2

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");
于 2012-01-04T19:50:25.350 回答
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
于 2015-08-06T13:59:22.517 回答