简体   繁体   中英

Runnable in while loop

I want my run method to be executed every 5 seconds while my program is running so I put it in a while(true) loop. The problem is that my application won't run because of this while loop. Can someone please explain how to properly code this loop so it will always be running?

Here is my code:

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);

}

Apparently the bug here is that the Runnable needs to reach an end point to start running. However, since it's in a while(true) loop it can never start running. Anybody know how to fix this?

EDIT: New Code thanks to JavaHunter but still has a bug:

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);

But this code just freezes my application. Here is the logcat:

在此处输入图片说明

It shows something about the GC being blocked but I do not know exactly what that means. Can someone please help?

What you are doing, is you are just creating infinitely many runnable objects(that are being GCed). You should put the while loop inside the runnable. And probably its better to change the loop condition to the following(It is more readable):

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;
                   } 
               }
        }
    };

while(true)放入run()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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