android - Thread to initiate thread creation in a service -
i want create executor fixed pool of 3 threads inside service.
those 3 threads work according data on sqlite db.
is there way tell threads "call" method on service tell him, "a thread done, can pull data db , start new thread"
that way can manipulate db , next threads act accordingly.
all managed fill queue data have on db, , way wont react changes on db cause pulled data
edit: code better understanding
public class mediadownloadservice extends service { public int onstartcommand(intent intent, int flags, int startid) { executor = new threadpoolexecutor(2,3,3000,timeunit.milliseconds,new linkedblockingqueue<runnable>()); //initiale start of new threads (first run) } public void startnewthread(){ if(helper.requestsexists()){ map<integer,string> requestmap = helper.getrequeststoexcute(0); set<integer> keyset = requestmap.keyset(); iterator<integer> iterator = keyset.iterator(); executor.submit(new mythread(file, getapplicationcontext(), iteratornext), 1); }else{ executor.shutdown(); this.stopself(); } }
and thread himself:
public class mythread implements runnable { private file _file; private context context; private dbhelper helper; private int requestid; public mythread(file file, context context, int requestid) { this._file = file; this.context = context; this.requestid = requestid; } @override public void run() { // thread work here helper.deleterequest(requestid);// remove db prevent infinite loop // question mediadownloadservice.startnewthread();// ??? can done } catch (ioexception e) { log.e("callable try", post.tostring()); } }
of course dont want static, there other way it?
i think have misconceptions here: executor.submit()
not accept thread
, runnable
. put runnable
queue , possibly (after time) assign thread perform actions specified runnable. whole executor exists not have thread creation, management , task scheduling yourself.
the threadpoolexecutor
implements queueing funtionality trying implement. 1 solution submit tasks executor come in. executor queue them , schedule them available threads.
also note there's asynctask.execureonexecutor()
gives possibility have onpostexecute()
running on main thread.
and please, rename mythread
, startnewthread()
else. former not thread, task , latter submits work executor.
Comments
Post a Comment