android - Why would a background thread spawn its own Handler & Looper -


why background thread spawn own handler & looper modify ui's component .i know in simple terms:

  • looper : loop , execute tasks in message queue

  • handler : posting tasks queue

have @ snippet took article in internet

public class myactivityv2 extends activity {     private handler muihandler = new handler();    private myworkerthread mworkerthread;     @override    protected void oncreate(bundle savedinstancestate) {       super.oncreate(savedinstancestate);       setcontentview(r.layout.activity_main);        mworkerthread = new myworkerthread("myworkerthread");       runnable task = 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(myactivityv2.this,                             "i @ middle of background task",                             toast.length_long)                             .show();                      }                   });                }             }             muihandler.post(new runnable() {                @override                public void run() {                   toast.maketext(myactivityv2.this,                       "background task completed",                       toast.length_long)                       .show();                }             });          }       };        // myworkerthread == handlerthread       mworkerthread.start();       mworkerthread.preparehandler();       // 2 starting of thread       mworkerthread.posttask(task);       mworkerthread.posttask(task);    }     @override    protected void ondestroy() {       mworkerthread.quit();       super.ondestroy();    } }  //myworkerthread.java  class myworkerthread extends handlerthread {     private handler mworkerhandler;     public myworkerthread(string name) {       super(name);    }     public void posttask(runnable task){       mworkerhandler.post(task);    }     public void preparehandler(){       mworkerhandler = new handler(getlooper());    } } 

it's either i'm misunderstanding code or lack of thread foundation in android. i'm sorry.

the background thread repeats (twice). main idea manipulation ui component through background thread.

have @ this:

mworkerhandler 

why background thread create own handler, if matter of manipulation ui's component, why doesn't take reference of ui thread handler , posting runnable through handler.

and

 mworkerhandler = new handler(getlooper()); 

which creating own looper (background thread's looper), indicates background thread creating own message queue. shouldn't supposed play around message queue of main thread , not background thread.

thanks in advance.

i can't vouch random article on internet, code using handlers correctly.

this line creates handler runs code on ui thread:

private handler muihandler = new handler(); 

this method creates handler runs code in background thread:

public void preparehandler(){   mworkerhandler = new handler(getlooper()); } 

these lines post runnable background thread:

mworkerthread.posttask(task); mworkerthread.posttask(task); 

so in nutshell, background thread use looper , handler same reason ui thread uses them: code on other threads can post messages , runnables it.


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 -