android - Thread and battery -


i have 1 problem , 2 options implementation , know 1 best in android environment (basicly best practice , 1 using less battery).

my problem: have 4 runnable implementations, runnable has list of tasks need executed in separate threads. runnable have no task long period of time , lot of tasks execute (each runnable executes tasks in sequential order), nothing long time , and on...

1st implementation

my fist implementation create 4 thread. each threads in charged of 1 runnable. each thread runs tasks, when list empty runnable pause thread (with wait()), when tasks added list thread waken (with notify()). here code of run method runnable:

  public void run() {     while (!terminated) {       while ( tasklist.size()> 0 ) {         task task = tasklist.get(0);         tasklist.remove(task);         handletask(task);       }        synchronized(tasklist) {          if (tasklist.size()==0)            tasklist.wait();       }     }   } 

first question: thread in pause state using ressource?

2nd implementation

in second implementation thinking of using executors.newcachedthreadpool() (as in asynctask implementation). basicly ask thread pool when runnable have tasks run , system in charge provide me available thread , destroy after period of inactivity. because 4 runnable have long period without tasks (execpt maybe 1 runnable) pretty sure behaviour of pool like:

  • always 1 thread busy
  • create 1 thread, delete 1 thread,
  • create 2 threads, delete 2 treads,
  • create 1 thread ...

threads never reused (except if set long period of inactivity).

so best option? having 4 threads spend of time in pause state or create / destroy lot of threads having them when need them.

thanks help,

jimboy.

keep threads in paused state. way, don't take resources. don't ever keep thread busy unless there's work done.

however, looking @ appear doing there, sounds may want subclass of blockingqueue.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -