问题
I know that it has similar topics. But in each topic we are open InputStream
before task will be run and in new thread load data. But I have 90 asset files. Is this a single way to create 90 AsyncTask
? Can I get access to main thread in inBackground()
method to get assets and open new InputStream
? Just in my case if I'll create 90 tasks I also need to synchronize their order of launch and finish. I need a point of synchronization where my AsyncTask
will be wait the main thread.
回答1:
My suggestion would be to have a Service with a ThreadPool executor and do your loading there, you can have a bunch of threads running in parallel to do your reading. When you are done you call back into your activity, you could use a broadcast, or a binder interface with a callback.
Another option is to have a fragment with setRetaininstance(true) and do it there with either AsyncTasks or a thread pool executor.
But do not do it in the activity with async tasks, if the user rotates the device you will have potentially 90 tasks running and no activity to return the results to.
来源:https://stackoverflow.com/questions/35881498/how-to-load-a-lot-of-files-from-assets-asynchronously