質問

So, I have the code below inside an AsyncTask and want to call 7 different asynchronous HTTP requests. All works well, all the 7 execute() methods start at the same time (give a take a few millis, which is great).

Unfortunately, the time it takes with this method is aprox. 16 secs. If I exclude all executor stuff and call the HTTP download methods on the original worker Asynctask, it takes aprox. 9 secs. So, it actually takes less time in sequential order rather than concurrent. Any ideas why this is happening ? Maybe something on the server side ? Maybe because the executors were started on an Asynctask ? Thanks a lot !

            MyExecutor executor = new MyExecutor(7, 7, 40000, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
            executor.execute(new Runnable()
            {
                @Override
                public void run()
                {
                    try {downloadSplashScreenJsonData();}
                    catch (Exception e)
                    {
                        Log.e(TAG, "Could not download splashscreen data.");
                        e.printStackTrace();
                    }
                }
            });
            // after another 6 executor.execute() calls,
            executor.shutdown();
            executor.awaitTermination(40000, TimeUnit.MILLISECONDS); 

    class MyExecutor extends ThreadPoolExecutor
    {


    public MyExecutor(int corePoolSize, int maximumPoolSize,
            long keepAliveTime, TimeUnit unit,
            BlockingQueue<Runnable> workQueue) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
        prestartAllCoreThreads();
        // TODO Auto-generated constructor stub

    }

    @Override
    public void execute(Runnable command) {
        super.execute(command);
        Log.e(TAG, "execute()");
        Log.e(TAG, "no of thr: " + getActiveCount());

    }
}
役に立ちましたか?

解決 2

As I look back on this matter, I want to add some more info.

First off, the use case that was required by the application was very retarded and cumbersome (but hey, clients, what can you do...). So like Joe stated above, I wouldn't download data on Asyncs in a million years now. One should use some sort of Service for downloading the data required, if possible.

Secondly, I ended up using RoboSpice library (it also provides caching) instead of Asyncs. It's still not as good as running on a Service, but it's much more well optimised than the barebone version. Might wanna check that out.

他のヒント

Don't know offhand, but I observe:

  1. What is restartAllCoreThreads, and why are you calling it in a constructor? Don't start threads before you need them (and a LinkedBlockingQueue<> will save you space).
  2. Do you really need to run this in an AsyncTask? The threads in a Threadpool don't run on the UI thread, and running off the UI thread is the main advantage of AsyncTask. If you really want to do all of this in the background, use an IntentService.
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top