的Java / Android的编程 - 环失败 [英] Java / Android Programming - Loop FAIL

查看:109
本文介绍了的Java / Android的编程 - 环失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个带计时器的while循环。 问题是,计时器不会在每次循环使用。 它仅用于第一次。经过第一次列入循环中的语句都没有,我已经设置了延迟执行。 如何是这甚至可能由于定时器被包含在while循环中。 任何解决办法?

 诠释计数= 1;
        而(计数< = 10){
            最终的处理程序处理程序=新的处理程序();
            计时器t =新的Timer();
            t.schedule(新的TimerTask(){
                公共无效的run(){
                    handler.post(新的Runnable(){
                        公共无效的run(){

                        //东西的,而循环的执行
                    }
                    });
                }
            },20000);
            算上++;
        }
 

解决方案

的TimerTask 揭开序幕一个新的线程,然后循环正常进行。

,线程的执行不会引起你的循环延迟来的code中的执行

I am using a while loop with a timer. The thing is that the timer is not used in every loop. It is used only the first time. After the first time the statements included inside the loop are executed without the delay that i have set. How is this even possible since the timer is included inside the while loop. Any solutions ?

        int count = 1;
        while (count <= 10) {
            final Handler handler = new Handler();
            Timer t = new Timer();
            t.schedule(new TimerTask() {
                public void run() {
                    handler.post(new Runnable() {
                        public void run() {

                        // Stuff the while loop executes
                    }
                    });
                }
            }, 20000);
            count++;
        }

解决方案

The TimerTask kicks off a new Thread and then the loop proceeds as normal.

The execution of the thread does not cause a delay to the execution of the code in your loop.

这篇关于的Java / Android的编程 - 环失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆