android - Matter of Handlers execution in a sequence -


i took snipet site explaining handler in android (a threading thing).

  @override        protected void oncreate(bundle savedinstancestate) {           super.oncreate(savedinstancestate);           setcontentview(r.layout.activity_main);            thread mythread = new thread(new runnable() {              @override              public void run() {                 (int = 0; < 4; i++) {                    try {                       timeunit.seconds.sleep(2);                    } catch (interruptedexception e) {                       e.printstacktrace();                    }                    if (i == 2) {                        muihandler.post(new runnable() {                          @override                          public void run() {                             toast.maketext(myactivity.this, "i @ middle of background task",                                 toast.length_long)                                 .show();                          }                       });                    }                 }//ends for()                 // second handler, right here!                 muihandler.post(new runnable() {                    @override                    public void run() {                       toast.maketext(myactivity.this,                           "background task completed",                           toast.length_long)                           .show();                    }                 });              } //ends run()           });           mythread.start(); 

judging task outputted in second executed handler,

 toast.maketext(myactivity.this,                       "background task completed",                       toast.length_long)                       .show(); 

seems writer of article pretty sure second handler executed last.

my question whether it's true second handler executed last after first handler finishes job. though, when ran multiple times, yes, it's executed last. in mind, since handler done in background thread we're not supposed know (even predict) 1 2 tasks handler execute first. need explanation, thank in advance.

the outermost anonymous runnable, 1 passed new thread(...) constructor, runs in background thread. inside runnable execute sequentially - 1 instruction after other.

since runnable has for loop , after final toast appears you're guaranteed it'll run after loop body.


Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

matplotlib support failed in PyCharm on OSX -

python - Matplotlib: TypeError: 'AxesSubplot' object is not callable -