android - get main thread's message queue and handler -


how message queue of main thread thread? looper.getmainlooper() gets main thread's looper unable find way messagequeue thread's looper. moreover, how handler main looper? unable find way it.

@r.v,

i had similar need. wanted know when messagequeue empty , when post , want know when becomes empty nothing remaining do. looked @ messagequeue.idlehandler , found didn't behave wanted came solution.

in case wanted use looper/handler mechanism sequentially execute file downloads. each download want execute wrapped in runnable. want 1 @ time running, pattern works without having dig nuts , bolts of more involved threading solution. additionally, wanted know when first put queue , begins work, , wanted know when done (queue empty).

i able use handler's message mechanism achieve this. messages handled in sequence runnables, can strategically place messages in queue know conditions of queue. unlike runnables in handler's queue, there query , removal abilities messages provide solution.

what each time add runnable handler (via handler.post), remove instances of custom queue_empty message, add fresh queue_empty message. ensures have queue_empty message @ end of queue. once encounter queue_empty message in subclassed handler, know i'm @ end of queue. additionally, if don't find queue_empty message in queue when go add runnable, know queue empty , thread idle.

as point out, there real inefficiencies solution. having iterate through queue these "marker" messages real performance issue if there large number of entries in queue. in case, i'm dealing handful of file downloads @ time performance penalties negligible. if have similar situation, think pretty reasonable solution. have been nice android sdk provide these basic abilities messagequeue. agree ideally wouldn't want mess messagequeue, knowing when idle/working/empty seem reasonable things , i'm sure there numbers of scenarios when there value knowing these things.

    class downloaderthread extends thread {     private static final int queue_empty = 9999;     private myhandler handler;      @override     public void run()     {         try         {             looper.prepare();             handler = new myhandler();             looper.loop();         }         catch (throwable t)         {             log.e(tag, "halted due error", t);         }     }      public void post(runnable r)     {         if(!handler.hasmessages(queue_empty))         {             log.v(tag, "download queue empty.  first element being added.");         }          handler.post(r);         handler.removemessages(queue_empty);         handler.sendemptymessage(queue_empty);     }      class myhandler extends handler     {         @override         public void handlemessage(message msg)         {             if(msg.what == queue_empty)             {                 log.v(tag, "download runnable queue empty!");             }         }     } }; 

Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -