Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 可在while循环中运行_Java_Android_Handler_Runnable - Fatal编程技术网

Java 可在while循环中运行

Java 可在while循环中运行,java,android,handler,runnable,Java,Android,Handler,Runnable,我希望我的run方法在程序运行时每5秒执行一次,因此我将其放入while(true)循环中。问题是我的应用程序不会运行,因为这是while循环。有人能解释一下如何正确编码这个循环,使它始终运行吗 这是我的密码: int i=0; while(true){ Runnable x = new Runnable(){ @Override public void run(){ if(my_list_counter <= 0)

我希望我的
run
方法在程序运行时每5秒执行一次,因此我将其放入
while(true)
循环中。问题是我的应用程序不会运行,因为这是
while
循环。有人能解释一下如何正确编码这个循环,使它始终运行吗

这是我的密码:

int i=0;    
while(true){
    Runnable x = new Runnable(){
        @Override
        public void run(){
            if(my_list_counter <= 0) 
                return;
            random_counter = rand.nextInt(my_list_counter);
            make_words_with_letters.add(list_of_letters.get(random_counter));
            for(Button b:button_list)
                if (b.getText().equals("")){
                    b.setText(list_of_letters.get(random_counter));
                    list_of_letters.remove(list_of_letters.get(random_counter));
                    my_list_counter--;
                    break;
                }

        }
    };
   i++;
   Handler handler = new Handler();
   //Run the code in runnable x at increasing time intervals of 5 seconds
   handler.postAtTime(x, SystemClock.uptimeMillis() + i*5000);

}
但这段代码只是冻结了我的应用程序。以下是日志:


它显示了GC被阻止的情况,但我不知道这到底意味着什么。有人能帮忙吗?

当(真)
放在
运行()里面
当(真)

当(真)
放在
运行()里面

你正在做的是,你正在创建无限多个可运行的对象(正在被GCed)。您应该将while循环放在runnable中。最好将循环条件更改为以下内容(可读性更强):


你正在做的是,你只是在创建无限多个可运行的对象(正在进行GCD)。您应该将while循环放在runnable中。最好将循环条件更改为以下内容(可读性更强):


可能尝试在run方法中添加while?现在您正在为每个while步骤创建一个新的runnable。不幸的是,仍然存在一个bug。请查看上面更新的代码,看看是否可以尝试在run方法中添加while?现在您正在为每个while步骤创建一个新的runnable。不幸的是,仍然存在一个bug。请查看上面更新的代码,看看您是否可以帮助我如何处理该处理程序?我希望run方法每5秒运行一次。但是它不适用于你的代码?我用你的代码和新的bug更新了我原来的帖子。你能帮忙吗?也谢谢你已经提供的帮助我该如何处理处理处理程序?我希望run方法每5秒运行一次。但是它不适用于你的代码?我用你的代码和新的bug更新了我原来的帖子。你能帮忙吗?也谢谢你已经帮了我的忙。请看我编辑过的帖子。仍然存在错误。删除while循环并使用SheduledThreadPoolExecutor方法sheduleAtFixedRate而不是Handler不起作用。请看我编辑过的帖子。仍然存在错误。请删除while循环并使用SheduledThreadPoolExecutor方法sheduleAtFixedRate而不是Handler
int i=0;    

    Runnable x = new Runnable(){
        @Override
        public void run(){
            while(true){
                if(my_list_counter <= 0) 
                    return;
                random_counter = rand.nextInt(my_list_counter);
                make_words_with_letters.add(list_of_letters.get(random_counter));
                for(Button b:button_list)
                    if (b.getText().equals("")){
                        b.setText(list_of_letters.get(random_counter));
                        list_of_letters.remove(list_of_letters.get(random_counter));
                        my_list_counter--;
                        break;
                    }

            }
        }
    };
   i++;
   Handler handler = new Handler();
   //Run the code in runnable x at increasing time intervals of 5 seconds
   handler.postAtTime(x, SystemClock.uptimeMillis() + i*50);
Runnable x = new Runnable(){
        @Override
        public void run(){
            while(my_list_counter > 0){ 
               random_counter = rand.nextInt(my_list_counter);
               make_words_with_letters.add(list_of_letters.get(random_counter));
               for(Button b:button_list)
                   if (b.getText().equals("")){
                       b.setText(list_of_letters.get(random_counter));
                       list_of_letters.remove(list_of_letters.get(random_counter));
                       my_list_counter--;
                       break;
                   } 
               }
        }
    };