ASyncTasks阻止其他人 [英] ASyncTasks blocking others

查看:55
本文介绍了ASyncTasks阻止其他人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个ASyncTasks,一个ASyncTasks从httpPost检索一个值,另一个更新UI的某些元素(包括列表视图). 问题在于,由于两个ASyncTasks共享同一个后台线程,因此如果网络运行首先启动并且运行缓慢(由于网络连接不良).其他后台线程花费太多时间,使应用程序不负责任.

I've 2 ASyncTasks, one retrieves a value from an httpPost and the other update some elements of the UI (including an listview). The problem is that since both ASyncTasks share the same background thread, if the network opperation start first and runs slow (due a bad network connectivity). The others background thread takes too much time making the app irresponsible.

由于两个ASyncTasks都是独立的,一个很愚蠢,一个又一个等待.不同的类使用不同的线程会更合乎逻辑,不是吗?

Since both ASyncTasks are independient is pretty stupid one to make wait the other. It would be more logical asynctasks different classes use different threads, am I wrong?

阅读 ASyncTask 文档.谈论使用 executeOnExecutor(),但是如何在低于11的API级别解决该问题?

Reading the ASyncTask doc. Talks about using executeOnExecutor(), but how can I solve that in a API level lower than 11?

这里有一个小例子,再现了问题"

Here goes a small example that reproduces the "problem"

        new Task1().execute();
        new Task2().execute();

使用

public class Task1 extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        GLog.e("doInBackground start 1");
        SystemClock.sleep(9000);
        GLog.e("doInBackground end 1");
        return null;
    }

    @Override
    protected void onPreExecute() {
        GLog.e("onPreExecute 1");
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(Void result) {
        GLog.e("onPostExecute 1");
        super.onPostExecute(result);
    }

}

public class Task2 extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        GLog.e("onPreExecute 2");
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... params) {
        GLog.e("doInBackground start 2");
        SystemClock.sleep(9000);
        GLog.e("doInBackground end 2");
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        GLog.e("onPostExecute 2");
        super.onPostExecute(result);
    }

}

推荐答案

这是我在代码中处理此问题的方式:

This is how I handle this in my code:

if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ) {
    new MyAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
    new MyAsyncTask().execute();
}

并将MyAsyncTask分别替换为您的Task1Task2.基本上,AsyncTask中的更改出现在Honeycomb中(请参见"此处执行顺序"部分),因此在此之前,请像往常一样启动它,对于HC及更高版本,如果您不喜欢新行为(我认为没有人这样做),请使用executeOnExecutor()

And replace MyAsyncTask with yours Task1 and Task2 respectively. Basically change in AsyncTask appeared in Honeycomb (see Android SDK docs here in "Order of execution" section), so before that, you launch it as usual, for HC and up, use executeOnExecutor() if you do not like new behaviour (noone does, I think)

这篇关于ASyncTasks阻止其他人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆