执行多个AsyncTask的的部件并联多个、部件、AsyncTask

2023-09-12 03:37:57 作者:走到路的尽头

我使用的Andr​​oid SDK 4.0.3 API15,我想平行运行多个AsyncTasks。我是从Web服务器和动画(10 fps)的它实时获取我的数据。但是,这取决于用户的操作,我需要一些数据发送到web服务器也。发生这种情况时我的动画停顿了很短的时间(发送数据进入队列和获取数据的等待它完成),因此我不能赶上实时的。

这回答是相当解释,但我不能使它工作。因此,任何帮助将是非常美联社preciated。

我想我需要使用此功能来实现的:

  AsyncTask.executeOnExecutor(遗嘱执行人EXEC,参数... PARAMS)
 

但我不能通过遗嘱执行人作为参数,我不能实例化一个执行者。这是我的AsyncTask类:

 公共类GetPlayers延伸的AsyncTask<字符串,太虚,字符串> {
    @覆盖
    保护字符串doInBackground(字符串...网址){

        。Thread.currentThread()setPriority(Thread.MAX_PRIORITY);

        RAWDATA =;
        对于(字符串网址:网址){
            DefaultHttpClient客户端=新DefaultHttpClient();
            HTTPGET HTTPGET =新HTTPGET(URL);
            尝试 {
                HTT presponse执行= client.execute(HTTPGET);
                InputStream的内容= execute.getEntity()的getContent()。

                的BufferedReader缓冲=新的BufferedReader(新InputStreamReader的(内容));
                如果((RAWDATA = buffer.readLine())== NULL){
                    RAWDATA =错误;
                }

            }赶上(例外五){
                e.printStackTrace();
            }
        }
        返回RAWDATA;
    }

    @覆盖
    保护无效onPostExecute(字符串结果){
        操作();
    }
}
 
Android之AsyncTask机制篇

和我执行它是这样的:

  GetPlayers任务=新GetPlayers();
requestString =网联系地址就在这里......;
task.execute(新的String [] {requestString});
 

解决方案

这是我如何做到这一点:

 如果(Build.VERSION.SDK_INT> = Build.VERSION_ codeS.HONEYCOMB){
    新MyAsyncTask()executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)。
} 其他 {
    新MyAsyncTask()执行()。
}
 

其中, MyAsyncTask 是有规律的AsyncTask的子类。或者,你可以用这一切的辅助类:

 类TaskHelper {

    公共静态< P,T扩展了AsyncTask的<?2 P,>>无效执行(T任务){
        执行(任务,(P [])NULL);
    }

    @燮pressLint(NewApi)
    公共静态< P,T扩展了AsyncTask的<?2 P,>>无效执行(T任务,P ... PARAMS){
        如果(Build.VERSION.SDK_INT> = Build.VERSION_ codeS.HONEYCOMB){
            task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,则params);
        } 其他 {
            task.execute(PARAMS);
        }
    }
}
 

然后就去做:

  TaskHelper.execute(新MyTask的());
 

  TaskHelper.execute(新MyTask的(),参数);
 

I'm using Android SDK 4.0.3 API15 and I want to run multiple AsyncTasks parallely. I'm getting my data from web server and animating(10 fps) it real-time. But depending on user operations I need to send some data to web server also. When this occurs my animation pauses for a short time (sending data gets into queue and getting data waits it to finish ) and therefore I can't catch the real-time.

This answer is quite explaining but I couldn't make it work. So any help will be very appreciated.

I think I need to use this function to achieve that:

AsyncTask.executeOnExecutor(Executor exec, Params... params)

But I can't pass an executor as a parameter and I can't instantiate an executor. This is my AsyncTask class:

public class GetPlayers extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {

        Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

        rawData="";
        for (String url : urls) {
            DefaultHttpClient client = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);
            try {
                HttpResponse execute = client.execute(httpGet);
                InputStream content = execute.getEntity().getContent();

                BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
                if((rawData = buffer.readLine()) == null){
                    rawData = "error";
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return rawData; 
    }

    @Override
    protected void onPostExecute(String result) {
        manipulate();
    }
}

And I execute it like this:

GetPlayers task = new GetPlayers();
requestString = "web adress is here...";
task.execute(new String[] { requestString });

解决方案

This is how I do that:

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

where MyAsyncTask is regular AsyncTask subclass. Or you can wrap this all in helper class:

class TaskHelper {

    public static <P, T extends AsyncTask<P, ?, ?>> void execute(T task) {
        execute(task, (P[]) null);
    }

    @SuppressLint("NewApi")
    public static <P, T extends AsyncTask<P, ?, ?>> void execute(T task, P... params) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
        } else {
            task.execute(params);
        }
    }
}

and then just do:

TaskHelper.execute( new MyTask() );

or

TaskHelper.execute( new MyTask(), args );