There is a piece of code where I use postDelayed and some other code executed on the main thread. I ran it a few times and always saw the following output:
07-13 14:22:18.511 15376-15376/sample1.com.sample_1 D/MainActivity: i = 0
....
07-13 14:22:18.601 15376-15376/sample1.com.sample_1 D/MainActivity: onResume 07-13 14:22:18.601 15376-15376/sample1.com.sample_1 D/MainActivity: postDelayed
As I see from the log output, it doesn't matter that my delay is 50 ms. The message "postDelayed" is typed after about 100 ms (601 - 511 = 90). It looks like the delayed runnable is added to the end of my UI thread's message queue. But anyway, is there any guarantee about when exactly postDelayed is typed? Can it be typed in the middle of the for loop?
package sample1.com.sample_1;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Log.d("MainActivity", "postDelayed");
}
}, 10);
for (int i = 0; i < 10000; i++) {
Log.d("MainActivity", "i = " + i);
}
}
@Override
protected void onResume() {
Log.d("MainActivity", "onResume");
super.onResume();
}
}