Thread.sleep within for loop causing issue with loop body

So in the code posted below I have a for loop which iterates 10 times, the goal of the for loop is to create the same ImageView 10 times every .5 seconds. When I tested this, it added everything at the same time after 5 seconds (assuming because it goes through the loop and waits 500ms to go through it again, 500ms * 10 = 5 seconds).

Thing is, why does it wait the 500ms 10 times in the loop THEN do the rest of everything in the for loop.

I added a Log.i print message to see if that would get printed at the same time, and if there was something wrong with my thread or for loop, and the message was printed every 500ms. I don't understand why it does the Log.i message every 500ms but the rest of the code inside the loop is done after when it's in the same brackets and both before the Thread.sleep method.

Thank you! Code: Main Code(The error is within the btnRoundsClick method)

    Variables var = new Variables();
EntityActions e = new EntityActions();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    final Window window = getWindow();
    window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.firstmap);
    FontTextView username = (FontTextView) findViewById(R.id.lblUsername);
    username.setText("NAME");
    RelativeLayout map1 = (RelativeLayout) findViewById(R.id.map1); 
    e.setMap(map1);
}

// On click of button, add enemies
public void btnRoundsClick(View view) {
    ImageView firetower[] = new ImageView[10];
    for (int c = 0; c < 10; c++) {
         try
                {
                firetower[c] = new ImageView(Start.this);
                  firetower[c].setImageResource(R.drawable.firetower);
                e.setEnemy(firetower[c]);
                e.setStartEnd(0, 0, 0, 198);
                e.addEnemy();
                e.followPath();
                Log.i("There  is 1 ", "more ImageView");
                Thread.sleep(500);
            } catch (Exception e)
            {
                e.printStackTrace();
            }
    }
}

EntityActions class ImageView enemy; int startX, startY, endX, endY; RelativeLayout map;

public EntityActions() {
    startX = 0;
    endX = 0;
    startY = 0;
    endY = 0;
}

public void setEnemy(ImageView enem) {
    enemy = enem;
}

public void setMap(RelativeLayout m) {
    map = m;
}

public void addEnemy() {
    map.addView(enemy);
    enemy.getLayoutParams().height = 60;
    enemy.getLayoutParams().width = 60;
    RelativeLayout.LayoutParams head_params = (RelativeLayout.LayoutParams)enemy.getLayoutParams();
    head_params.setMargins(30, -20, 0, 0); //substitute parameters for left, top, right, bottom
    enemy.setLayoutParams(head_params);
}

public void setStartEnd(int xS, int xE, int yS, int yE) {
    startX = xS;
    endX = xE;
    startY = yS;
    endY = yE;
}

public void followPath() {
    TranslateAnimation animation = new TranslateAnimation(startX, endX, startY, endY);
    animation.setDuration(6000);
    animation.setFillAfter(true);
    ((View) enemy).startAnimation(animation);
}

If I have missed anything or you have further questions, let me know.

Jon Skeet
people
quotationmark

Thing is, why does it wait the 500ms 10 times in the loop THEN do the rest of everything in the for loop.

It doesn't. It executes the iterations of the loop, once every 500ms - and then you allow the UI thread to actually display the updates. Add some logging in the loop and you'll see that "one iteration per 500ms" behaviour.

Basically, you're blocking the UI thread. Don't do that.

Read the Android Processes and Threads guide for more details and alternatives.

people

See more on this question at Stackoverflow