Вопрос

What i am trying to achieve is share data between running asynctask (doInBackground). So whats happening is that i have separate class (extends Asynctask) that loads data and activity with its own Async class which i use to update info in the activity. Basicly what i am trying to achieve is the thread in the activity (monitoring thread) to work alongside with the loader and providing some data from the loader class and when the loader is ready both the "monitoring" and the "loader" should die.

I tried having a volatile variable which I set using interfaces, but no success i cant seem to be able to share information between the threads(asynctasks).. Any suggestions? Maybe an Exchanger class is needed?

Это было полезно?

Решение

Any reason you can't use the onProgressUpdate functionality? I may be confused by your use case... but an example might be the following:

class MyActivity extends Activity {

  private AsyncTask<Uri,MyDataObject, MyResult> = new AsyncTask<Uri,MyDataObject, MyResult>() {
    private MyResult mResult;

    protected MyResult doInBackground(Uri... uris) {           
      int count = urls.length;
      mResult = new MyResult()

      for (int i = 0; i < count; i++) {
        MyDataObject anObject = mDataLoader.getObject(uris[i]);
        publishProgress(anObject);

        mResult.add(anObject);
        // Escape early if cancel() is called
        if (isCancelled()) break;
      }
      return totalSize;
    }

    protected void onProgressUpdate(MyDataObject... data) {
      addDataToUI(data[0]);
    }

    protected void onPostExecute(MyResult result) {
      Toast.makeText("All Done!", Toast.LENGTH_LONG).show();
    }
  }
}

Другие советы

It sounds like you want to have the Activity show some information as the data is loaded by the loader thread? So you need to pass some information from the loader into the Activity, updating the UI. In this case I'm not sure you need a second AsyncTask (and associated thread) in the Activity, as you still need to get the data to the main thread before you can update the UI. Instead, you should do as JRaymond suggests and just push the changes to the main thread using publishProgress and handle them on the main thread in onProgressUpdate

If you need something more general, you could also consider using java.util.concurrent. For example, a BlockingQueue can be used to implement the consumer/producer pattern. See the Javadoc for a code snippet.

All async tasks (unless executed with the executor provided) run on the same background thread, so it is not possible to execute them concurrently.

From documentation:

When first introduced, AsyncTasks were executed serially on a single background thread. Starting with DONUT, this was changed to a pool of threads allowing multiple tasks to operate in parallel. Starting with HONEYCOMB, tasks are executed on a single thread to avoid common application errors caused by parallel execution.

If you truly want parallel execution, you can invoke executeOnExecutor(java.util.concurrent.Executor, Object[]) with THREAD_POOL_EXECUTOR.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top