【问题标题】:How to make timer task to wait till runOnUiThread completed如何使计时器任务等到 runOnUiThread 完成
【发布时间】:2012-02-02 17:10:42
【问题描述】:

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

【问题讨论】:

    标签: java android multithreading timertask ui-thread


    【解决方案1】:

    试试这个

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

    【讨论】:

    • 在等待之前发生通知的可能性很小。我宁愿在这里使用倒计时。 (或在同步块中包含 runonuithread 调用以确保在等待之前不调用通知)
    【解决方案2】:

    我认为最简单的方法是使用“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
    

    【讨论】:

      猜你喜欢
      • 2011-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多