Alternative to Android AsyncTask
I have a UI widget where I need to map addresses to corresponding contact names and display them in a given format. I use AsyncTask to perform the que开发者_如何学Pythonry to lookup the address in doInbackground() and display the name format on the widget in onPostExecute(). This works fine when there is a limit on the addresses. But when number of addresses to be mapped increase..in the order of 150, I get RejectedExecutionException. I understand that this is because maxPoolsize is 128 and queue is bounded to 10 for AsyncTask. I tried making the queue unbounded...but adding of names on the widget becomes very slow. That is because with unbounded queue maxPoolSize is ignored and at a time I can have only corePoolSize threads i.e 5.
So, I wanted to know if there is any alternative other than AsyncTask that I can use for this use-case?? Cloning AsyncTask & increasing core/max pool sizes does not seem to be good behavior as that would mean many simultaneous threads(ineffective resource usage) & scheduling overhead. There is currently no limit on the addresses, and hence it could easily goto more than 500. How should I handle such cases ?
One approach could be to divide the mapping of this approximately 500 addresses in smaller parts of around 80 and then use a separate AsyncTask for every part.
Another alternative I would recommend is to use a Service that does this work for you.
精彩评论