title: order and timing of command execution bug
body: <p>I'm making a "Simon says" game as my final project for highschool, and it's mostly done but I have one problem.</p>
  
  <p>Currently, I'm trying to make the buttons light when I press them.
  However, they don't seem to light up and the sleep function doesn't really work.
  They only change color when the function that displays the next sequence of colors lights up.</p>
  
  <p>Here's the code:</p>
  
  <pre><code> package com.gabie212.simonsays;
  
  import android.content.Context;
  import android.content.Intent;
  import android.content.SharedPreferences;
  import android.media.MediaPlayer;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  import android.widget.TextView;
  
  import java.util.ArrayList;
  
  public class GameActivity extends AppCompatActivity implements View.OnClickListener {
  
      private int i = 0, pNum = 0, pIndex = 0,score;
      private Thread t = new Thread();
      private Thread bt = new Thread();
      private Button greenButton;
      private Button redButton;
      private Button blueButton;
      private Button yellowButton;
      private Button startButton;
      private TextView Score;
      private boolean startActivated = false;
      private MediaPlayer greenBeep;
      private MediaPlayer redBeep;
      private MediaPlayer blueBeep;
      private MediaPlayer yellowBeep;
  
      private ArrayList&lt;Integer&gt; userColors = new ArrayList&lt;Integer&gt;();
  
      private GameManger gm;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
  
          setContentView(R.layout.activity_game);
  
          Score = (TextView) findViewById(R.id.ScoreNum);
          greenButton = (Button) findViewById(R.id.btnGreen);
          redButton = (Button) findViewById(R.id.btnRed);
          blueButton = (Button) findViewById(R.id.btnBlue);
          yellowButton = (Button) findViewById(R.id.btnYellow);
          startButton = (Button) findViewById(R.id.btnStart);
          greenButton.setOnClickListener(this);
          redButton.setOnClickListener(this);
          blueButton.setOnClickListener(this);
          yellowButton.setOnClickListener(this);
          startButton.setOnClickListener(this);
          greenBeep = MediaPlayer.create(this, R.raw.greenbeep);
          redBeep  = MediaPlayer.create(this, R.raw.redbeep);
          blueBeep =   MediaPlayer.create(this, R.raw.bluebeep);
          yellowBeep =  MediaPlayer.create(this, R.raw.yellowbeep);
       /*
          SharedPreferences sp = getSharedPreferences("score", Context.MODE_PRIVATE);
          SharedPreferences.Editor editor = sp.edit();
          editor.clear();
          editor.apply();
  
       */
  
      }
  
      public void start() {
          startActivated=true;
          gm = new GameManger(this);
          Score.setText("0");
          lightUp(0);
      }
  
      public void beepStop(){
          greenBeep.stop();
          redBeep.stop();
          blueBeep.stop();
          yellowBeep.stop();
  
      }
  
  
      public void lightUp(final int i) {
  
          android.os.Handler handler = new android.os.Handler();
          if (i &lt; gm.getRandomColors().size())  //light up code
          {
              switch (gm.getRandomColors().get(i)) {
                  case 1:
                      greenButton.setBackgroundResource(R.drawable.greenlightup);
                      greenBeep.start();
                      handler.postDelayed(new Runnable() {
  
                          @Override
                          public void run() {
                              // TODO Auto-generated method stub
                              greenButton.setBackgroundResource(R.drawable.green);
                              lightUp(i+1);
                          }
                      }, 500);
  
  
                      break;
                  case 2:
                      redButton.setBackgroundResource(R.drawable.redlightup);
                      redBeep.start();
                      handler.postDelayed(new Runnable() {
  
                          @Override
                          public void run() {
                              // TODO Auto-generated method stub
                              redButton.setBackgroundResource(R.drawable.red);
                              lightUp(i+1);
                          }
                      }, 500);
  
  
                      break;
                  case 3:
                      blueButton.setBackgroundResource(R.drawable.bluelightup);
                      blueBeep.start();
                      handler.postDelayed(new Runnable() {
  
                          @Override
                          public void run() {
                              // TODO Auto-generated method stub
                              blueButton.setBackgroundResource(R.drawable.blue);
                              lightUp(i+1);
                          }
                      }, 500);
  
  
                      break;
                  case 4:
                      yellowButton.setBackgroundResource(R.drawable.yellowlightup);
                      yellowBeep.start();
                      handler.postDelayed(new Runnable() {
  
                          @Override
                          public void run() {
                              // TODO Auto-generated method stub
                              yellowButton.setBackgroundResource(R.drawable.yellow);
                              lightUp(i+1);
                          }
                      }, 500);
  
  
                      break;
              }
  
          }
          pIndex = 0;
      }
  
  
      @Override
      public void onClick(View v) {
          if (v.getId() == startButton.getId()) {
              start();
          } else {
              if (startActivated) {
                  if (v.getId() == greenButton.getId()) {//here is the part where i try to make the buttons change colors
                      greenBeep.start();
                      greenButton.setBackgroundResource(R.drawable.greenlightup);
                      try {
                          t.sleep(1000);
                          // Do some stuff
                      } catch (Exception e) {
                          e.getLocalizedMessage();
                      }
                      greenButton.setBackgroundResource(R.drawable.green); 
                      pNum = 1;
                  }
                  if (v.getId() == redButton.getId()) { //here is the part where i try to make the buttons change colors
                      redBeep.start();
                      redButton.setBackgroundResource(R.drawable.redlightup);
                      try {
                          t.sleep(1000);
                          // Do some stuff
                      } catch (Exception e) {
                          e.getLocalizedMessage();
                      }
                      redButton.setBackgroundResource(R.drawable.red);  
                      pNum = 2;
                  }
                  if (v.getId() == blueButton.getId()) { //here is the part where i try to make the buttons change colors
                      blueBeep.start();
                      blueButton.setBackgroundResource(R.drawable.bluelightup);
                      try {
                          t.sleep(1000);
                          // Do some stuff
                      } catch (Exception e) {
                          e.getLocalizedMessage();
                      }
                      blueButton.setBackgroundResource(R.drawable.blue); 
                      pNum = 3;
                  }
                  if (v.getId() == yellowButton.getId()) {//here is the part where i try to make the buttons change colors
                      yellowBeep.start();
                      yellowButton.setBackgroundResource(R.drawable.yellowlightup);
                      try {
                          t.sleep(1000);
                          // Do some stuff
                      } catch (Exception e) {
                          e.getLocalizedMessage();
                      }
                      yellowButton.setBackgroundResource(R.drawable.yellow); 
                      pNum = 4;
  
                  }
                  if (!gm.check(pNum, pIndex)) {
                      beepStop();
                      SharedPreferences sp = getSharedPreferences("score", Context.MODE_PRIVATE);
                      Intent i = null;
                      score = gm.getRandomColors().size()-1;
                      if(score &gt; sp.getInt("scoreP3",0)) {
                           i = new Intent(GameActivity.this, InsertScoreActivity.class);
                          i.putExtra("score", gm.getRandomColors().size() - 1);
                          startActivity(i);
                      }
                      else {
                          i = new Intent(GameActivity.this, GameOverActivity.class);
                          i.putExtra("score", gm.getRandomColors().size() - 1);
                          startActivity(i);
                      }
                  }
                  pIndex++;
                  if (pIndex == gm.getRandomColors().size()) {
                      Score.setText("" + gm.getRandomColors().size() + "");
                      gm.addColor();
                      try {
                          t.sleep(1000);
                          // Do some stuff
                      } catch (Exception e) {
                          e.getLocalizedMessage();
                      }
                      lightUp(0);//it either performs the color change here beofre the lightup function or it just sleeps and doesn't perform the color change at all
                  }
              }
  
          }
  
      }
  }
  </code></pre>
  
  <p>here's the game helper class:</p>
  
  <pre><code>package com.gabie212.simonsays;
  
  import java.util.ArrayList;
  import java.util.Random;
  
  /**
   * Created by Ronit on 21/02/2018.
   */
  
  public class GameManger {
  
      private GameActivity gActivity;
      static Random rnd = new Random();
      private ArrayList&lt;Integer&gt; randomColors;
  
  
      public GameManger(GameActivity mA)
      {
          randomColors = new ArrayList&lt;Integer&gt;();
          this.gActivity =mA;
          randomColors.add(getColor());
  
      }
  
      public void addColor()
      {
          randomColors.add(getColor());
      }
  
      public int getColor()
      {
          return rnd.nextInt(4)+1;
      }
  
      public ArrayList&lt;Integer&gt; getRandomColors()
      {
          return randomColors;
      }
  
  
      public boolean check(int num, int index)
      {
          return(randomColors.get(index)==num);
      }
  }
  enter code here
  </code></pre>
  
  <p>I have some problems.</p>
  
  <ol>
  <li>The sleep doesn't perform, does it have something to do with the way I did it in the if statements? Sleep just doesn't happen in the code I have written.</li>
  <li>Color change doesn't happen for some reason and I don't know why it doesn't, maybe it has something to do with the <code>thread.sleep</code>.</li>
  <li>Color change happens later in the program than its supposed to. Again, I think it has something to do with the sleep but when I removed the sleep, the color change still happens later in the code.</li>
  </ol>