执行的AsyncTask的doInBackground中的一个线

执行的AsyncTask的doInBackground中的一个线

本文介绍了执行的AsyncTask的doInBackground中的一个线程()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个的AsyncTask 在做网络操作。我最近问的增加,所有这些操作,另一个网络调用,调用远程服务发送的一些统计数据之后。我想这个统计称不耽误主要网络通话,所以我想在创建的线程执行这些而起家的的AsyncTask 的<$内C $ C> doInBackground()。该线程将在 doInBackground(),可能整个的AsyncTask 结束后,最有可能结束。我想这和它的作品,但如果有副作用,如内存泄漏,或类似的?

I have several AsyncTasks doing network operations. I was recently asked to add, after each of these operations, another network call that invokes a remote service sending some statistics. I'd like this statistics call not to delay 'main' network calls, and so I was thinking of executing them in a thread created and started inside the AsyncTask's doInBackground(). This thread would most probably end after the doInBackground() and possibly the whole AsyncTask has ended. I tried this and it works, but I was wondering if there are side effects, such as memory leaks or similar?

感谢;)

推荐答案

如果你想开始在线程 doInBackground 方法,你可以在 onProgressUpdate()法

If you want to start thread in doInBackground method, you can start it in onProgressUpdate() method

例如:

protected class MyTask extends AsyncTask<Void,Integer,Void> {

        public static final int START_THREAD = -1;
        @Override
        protected void onProgressUpdate(Integer... values) {
            if(values[0] == START_THREAD){
                startThread();
            }
        }

    @Override
    protected Void doInBackground(Void... params) {
        publishProgress(START_THREAD);
        return null;
    }
}

这篇关于执行的AsyncTask的doInBackground中的一个线程()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 18:10