android - Weird behavior with asynctask -
so coming across weird problem cant find explaination for. have async task in in dobackground method wait until variable set "wait" notified
private class testasynctask extends asynctask<void, object, boolean> { @override protected void onpreexecute() { log.d("test1"); } @override protected boolean doinbackground(void... params) { log.d("test2"); while (nextcardtoplay == null) { wait(); } log.d("test3"); } }
activity a:
protected void oncreate(){ = new testasynctask().execute(); } protected void onpause(){ a.cancel() }
so can see when activity starts, asynctask started. when activity closed asynctask supposed cancelled.
what noticed if open activity, close it, , reopen again asynctask created , in wait mode (never cancelled). no problem. whats confusing when start activity (while stale asynctask there), seems new asynctask started ( because logs onpreexecute called) doinbackground in nextasynctask not executed because test2 log not showing.
any idea why?
this behavior not @ weird if @ documentation, states asynctasks
run on single background thread, i.e. sequentially. if want tasks run on parallel worker threads, use executeonexecutor()
method instead of simple execute()
, pass asynctask.thread_pool_executor
parameter.
Comments
Post a Comment