停止循环,直到方法调用onFinished()

时间:2018-11-03 18:54:01

标签: java loops for-loop countdowntimer

如何停止for循环并等待该方法完成并在调用onFinish()后继续执行?

我能想到的一种解决方案是使用listeners并在方法调用onFinish()时调用它,但是我不知道如何停止循环并等待listener。同样,wait()之类的功能将无法正常工作,因为doCountDown()所花费的时间可能多于wait()

for (int i = 0; i < 1; i++) {
    doCountDown();
    //Pause and wait for doCountDown() to finish
}

private void doCountDown(){

        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                new CountDownTimer(30000,100) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        //Do some stuff
                    }

                    @Override
                    public void onFinish() {
                        //Continue loop
                    }
                }.start();
            }
        });
}

4 个答案:

答案 0 :(得分:1)

您可以使用CyclicBarrier。它是可重用的,因此您不必每次都创建它。

// this will create barrier for 2 parties.
CyclicBarrier barrier = new CyclicBarrier(2);
for (int i = 0; i < count; i++) {
    doCountDown();
    //Pause and wait for doCountDown() to finish
    // add try catch around for interrup exception
       barrier.await();
}
.......
@Override
public void onFinish() {
     ////Continue loop
      barrier.await();
}

答案 1 :(得分:1)

它与Object.waitObject.notify方法一起使用。

// Whatever object
Object obj = new Object();

        for (int i = 0; i < 1; i++) {
            doCountDown(obj);
            // Pause and wait for doCountDown() to finish
            synchronized (obj) {
                try {
                    obj.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

    private void doCountDown(Object obj) {

        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                new CountDownTimer(30000, 100) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        // Do some stuff
                    }

                    @Override
                    public void onFinish() {
                        // Continue loop
                        synchronized (obj) {
                            obj.notify();
                        }
                    }
                }.start();
            }
        });
    }

答案 2 :(得分:1)

以下是您对仅实现1个计时器的评论的答案:

private void doCountDowns(int count){

    new Handler(Looper.getMainLooper()).post(new Runnable() {
        @Override
        public void run() {
            int timer = 0; // if each timer does something else
            int ticks = 0;
            new CountDownTimer(30000 * count,100) {
                @Override
                public void onTick(long millisUntilFinished) {
                    //Do some stuff
                    if ((ticks++) == 3000) {
                        ticks = 0;
                        timer++;
                    }
                }

                @Override
                public void onFinish() {
                    // called only once, when everything is done
                }
            }.start();
        }
    });
}

doCountDowns(10)将启动一个10x 30000ms的计时器,滴答时间为3000x(每100ms)。可以解决您的问题吗?如果您的每个计时器都有另一个任务(但是似乎不是这种情况),则可以引入tickstimer字段来计算滴答计数和计时器。

也许您可以尝试解释一下,为什么您需要10个计时器在30.000 ms的时间内滴答,而不是1个计时器在300.000 ms的滴答中滴答。

答案 3 :(得分:0)

您还可以使用递归来获得所需的结果。

doAnotherLine(0);

private void doAnotherLine(int i){
    //Some stopper method.. for example something like this
    if(i < list.size -1) doCountDown(i);

}

private void doCountDown(final int i){

        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                new CountDownTimer(30000,100) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        //Do some stuff
                    }

                    @Override
                    public void onFinish() {
                        doAnotherLine(i+1);
                    }
                }.start();
            }
        });
}