multithreading - Android AsyncTask threads limits? -
i developing application need update info every time user logs in system, use database in phone. operations (updates, retrieving data db , etc.) use async tasks. till didn't see why shouldn't use them, experienced if operations of async tasks stop on pre-execute , don't jump doinbackground. strange leave that, developed simple application check whats wrong. , strange enough, same behavior when count of total async tasks reach 5, 6th 1 stops on pre-execute.
does android have limit of asynctasks on activity/app? or bug , should reported? did experience same problem , maybe found workaround it?
here code:
simply create 5 of threads work in background:
private class longasync extends asynctask<string, void, string> { @override protected void onpreexecute() { log.d("testbug","onpreexecute"); isrunning = true; } @override protected string doinbackground(string... params) { log.d("testbug","doinbackground"); while (isrunning) { } return null; } @override protected void onpostexecute(string result) { log.d("testbug","onpostexecute"); } }
and create thread. enter preexecute , hang (it not go doinbackground).
private class testbug extends asynctask<string, void, string> { @override protected void onpreexecute() { log.d("testbug","onpreexecute"); waiting = new progressdialog(testactivity.this); waiting.setmessage("loading data"); waiting.setindeterminate(true); waiting.setcancelable(true); waiting.show(); } @override protected string doinbackground(string... params) { log.d("testbug","doinbackground"); return null; } @override protected void onpostexecute(string result) { waiting.cancel(); log.d("testbug","onpostexecute"); } }
all asynctasks controlled internally shared (static) threadpoolexecutor , linkedblockingqueue. when call execute
on asynctask, threadpoolexecutor
execute when ready time in future.
the 'when ready?' behavior of threadpoolexecutor
controlled 2 parameters, core pool size , maximum pool size. if there less core pool size threads active , new job comes in, executor create new thread , execute immediately. if there @ least core pool size threads running, try queue job , wait until there idle thread available (i.e. until job completed). if not possible queue job (the queue can have max capacity), create new thread (up-to maximum pool size threads) jobs run in. non-core idle threads can decommissioned according keep-alive timeout parameter.
before android 1.6, core pool size 1 , maximum pool size 10. since android 1.6, core pool size 5, , maximum pool size 128. size of queue 10 in both cases. keep-alive timeout 10 seconds before 2.3, , 1 second since then.
with of in mind, becomes clear why asynctask
appear execute 5/6 of tasks. 6th task being queued until 1 of other tasks complete. reason why should not use asynctasks long-running operations - prevent other asynctasks ever running.
for completeness, if repeated exercise more 6 tasks (e.g. 30), see more 6 enter doinbackground
queue become full , executor pushed create more worker threads. if kept long-running task, should see 20/30 become active, 10 still in queue.
Comments
Post a Comment