I'm trying to run a runnable from another runnable using Handler.post() method of my singleton but this second runnable is not being run until after the original runnable finishes. In the example code below I am calling MyManager,getInstance().startCommand() from somewhere in the application. The myRun variable in the MyCommand class has a sleep put on the thread so I can test a timeout feature, but the runnable called mTimeoutTimer is not be executed until AFTER the myRun runnable is finished. Why is this happening, and how can I change that?
public class MyManager{
private MyManager sInstance;
private Handler mHandler;
private Runnable mTimeoutTimer;
public static MyManager getInstance(){
if(sInstance == null){
sInstance = new MyManager();
}
return sInstance;
}
private MyManager(){
mHandler = new Handler();
mTimeoutTimer = new Runnable() {
@Override
public void run() {
Log.e(“RUNNABLE RUNNING!”);
}
};
public class MyCommand {
private Runnable myRun;
public MyCommand(){
myRun = new Runnable() {
@Override
public void run() {
MyManager.getInstance().startTimeoutTimer();
try {
Thread.sleep(COMMAND_TIMEOUT_MILLIS * 3);
} catch (InterruptedException e) {}
MyCommand.this.execute();
}
};
}
public void execute() {
myRun.run();
}
}
private void startTimeoutTimer(){
mHandler.postDelayed(mTimeoutTimer);
}
public void startCommand(){
new MyCommand().execute();
}
}