Java线程信令不会将执行返回到循环

时间:2014-04-05 06:06:35

标签: java android multithreading

我有一个在Tread中实例化并启动的类。代码如下:

private class StartCycle implements Runnable {

    @Override
    public void run() {
        ...
        while(pomodoroLeft > 0) {
            ...
            synchronized(runnable){
                try{
                  runnable.wait();
                } catch(InterruptedException e){}
            }
            runTime = shortLength * ONE_MINUTE; //These statements are not getting 
            runnable = new Clock();             //executed
            runnable.run();


        }
    }


} 

实例名称为runnable的时钟类如下:

private class Clock implements Runnable
{

   public void run() {
                ...
                synchronized (this) {
                    runnable.notifyAll();
                }
                //here goes the alarm.
            }


    }
}

由于某种原因,第一个线程的执行不会返回到其路径和语句之后。我弄乱了通知吗?或者还有其他原因吗?

1 个答案:

答案 0 :(得分:0)

如果您正在构建警报/计时器,只需使用内置的AlarmManager

AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, (1000 * 60), (1000 * 60), yourPendingIntent);

最有可能的是,如果你自己构建,你的应用程序将占用大部分的cpu,这将导致用户设备的低位消耗

Alarm Manager API