简体   繁体   English

如何强制while循环一次执行一个可运行的循环。

[英]How to force a while loop execute one runnable at a time.

I am trying to make a simple timer with uneven time intervals after each repetition. 我正在尝试制作一个简单的计时器,每次重复后的时间间隔不均匀。

I start as follows: 我开始如下:

            case R.id.start:
                timerRuns = true;
                startCycle();
                break;

The cycle itself looks like this: 循环本身看起来像这样:

private void startCycle() {
        pomodoroLeft = numPomodoro;
        while(pomodoroLeft > 0) {
            pomodoroLeft--;
            actualSeconds = pomodoroLength * ONE_MINUTE;
            setTimeAndRun(actualSeconds);
            actualSeconds = shortLength * ONE_MINUTE;
            setTimeAndRun(actualSeconds);


        }
    }

Method call: 方法调用:

 private void setTimeAndRun(long timePeriod) {
        runTime = timePeriod;
        runnable.run();
    }

And finally runnable itself: 最后可以运行:

private Runnable runnable = new Runnable()
    {

        public void run() {

            if (timerRuns) {
                runTime -= ONE_SECOND;
                String str = String.format("%1$02d : %2$02d",
                        TimeUnit.MILLISECONDS.toMinutes(runTime),
                        TimeUnit.MILLISECONDS.toSeconds(runTime) -
                                TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(runTime))
                );
                timeShown.setText(str);

                mHandler.postDelayed(this, 1000);
                if(timeShown.getText().toString().contentEquals("00 : 00")) {
                    stopClock();
                    //here goes the alarm.
                }
            }

        }
    };

My problem is that when I start the timer while loop seems to execute everything despite incompliete run() of the previous method call. 我的问题是,尽管启动了前一个方法调用的run(),但当我启动计时器时,while循环似乎可以执行所有操作。 As a consequence timeShown TextView displays this actualSeconds = shortLength * ONE_MINUTE right away and skips 1 second each second because 2 runnables are running at the same time. 结果,timeShown TextView立即显示了此actualSeconds = shortLength * ONE_MINUTE ,并每秒跳过1秒,因为2个可运行对象同时运行。

I want to achieve sequential execution here. 我想在这里实现顺序执行。 What would be the best way to do so? 最好的方法是什么? Maybe implementing non-anonymous subclass and instantiate it every time would help? 也许实现非匿名子类并每次实例化都会有所帮助?

Also, if you have any other suggestions that would improve my code I would greatly appreciate. 另外,如果您还有其他建议可以改善我的代码,我将不胜感激。

You should take a look at queues. 您应该看看队列。

Here is a link to a similar question: 这是一个类似问题的链接:

How to implement a queue of runnables 如何实现可运行队列

You should use the Executors.newSingleThreadExecutor() 您应该使用Executors.newSingleThreadExecutor()

http://developer.android.com/reference/java/util/concurrent/Executors.html#newSingleThreadExecutor%28java.util.concurrent.ThreadFactory http://developer.android.com/reference/java/util/concurrent/Executors.html#newSingleThreadExecutor%28java.util.concurrent.ThreadFactory

And here is a tutorial about the Executor: 这是关于执行器的教程:

http://tutorials.jenkov.com/java-util-concurrent/executorservice.html http://tutorials.jenkov.com/java-util-concurrent/executorservice.html

here is also something that may help you understanding better multithreading in java: 这也可以帮助您更好地理解Java多线程:

Understanding multi-threading 了解多线程

hope this helps somehow. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM