Runnable无法更新TextView

时间:2014-04-05 23:19:58

标签: android multithreading debugging android-emulator

真的很奇怪,我不知道在这里想什么。

我有一个小的计时器程序,它创建一个具有某些操作的线程。其中一个操作是创建另一个线程来执行实际倒计时。

问题是,当我只是运行程序时,它无法按预期工作(计时器在05:00冻结)。但是,当我尝试在调试模式下执行操作并在可疑行中设置断点时,执行会通过!它按预期计算低于5分钟,但有一点警告信息:

I/PomoACt﹕ 04 : 56
I/Choreographer﹕ Skipped 57 frames!  The application may be doing too much work on its main
I/PomoACt﹕ 04 : 55
I/Choreographer﹕ Skipped 58 frames!  The application may be doing too much work on its main thread.

来自编舞者的消息可能是由于模拟器的缓慢,但为什么常规与调试模式的执行流程有差异?

编辑: 根据要求发布代码。

这是我在主UI线程中运行的实例的线程:

@Override
        public void run() {

            pomodoroLeft = numPomodoro;
            while(pomodoroLeft > 0) {
                pomodoroLeft--;
                runTime = TWENTY_MINUTES; //runTime is needed for timerUpdate
                timerUpdate();                         //This method just updates 
                                                       //textWiew with the current value of 
                                                       //runTime
                runnable = new Clock(); 
                timerRun = true; 
                runnable.run(); 
                waitForClock();
                Log.i("while", "execution in while");


                runTime = FIVE_MINUTES;
                Log.i("while", "execution in while 2");
                runnable = new Clock();
                timerRuns = true;
                Log.i("while", "execution in while 3");
                timerUpdate();
                runnable.run();                          //This is form where the execution
                                                         //misbehaves
                waitForClock();



public class Clock implements Runnable 
    {

        public void run() {
            if (!timerRuns) {
                return;
            }
                Log.i("PomoACt", timeShown.getText().toString());   //To log currently 
                                                                    //displayed value
                runTime -= ONE_SECOND;
                timerUpdate();

                mHandler.postDelayed(this, 1000);                   //Wait a second
                if(timeShown.getText().toString().contentEquals("00 : 00")) {
                    stopClock();
                    synchronized (runnable) {
                        runnable.notify();                          //Notify the waiting
                                                                    //thread that it should
                                                                    //go on with execution
                        Log.i("Not", "notified from runnabe");
                    }
                    //here goes the alarm.
                }


        }
    }

public void stopClock() {
        timerRuns = false;
        mHandler.removeCallbacks(runnable);
    }


 public void waitForClock() {
        synchronized(runnable){
            try{
                runnable.wait();                     //Wait for the current runnable to 
                                                     //compliete
            } catch(InterruptedException e){}
        }
    }

所以TWENTY_MINUTES循环按预期执行,但FIVE_MINUTE循环没有。它只是 执行一个,因此在模拟器上显示4:59。它在调试会话期间执行正常。

0 个答案:

没有答案