Sagar
Sagar

Reputation: 15

Waiting for threadpool to complete execution

I am new to threading . I have searched many questions related to my problem but I am not able to find the right solution for me. What I am doing is I am using four async tasks to fetch data from four different social media using the THREAD_POOL_EXECUTOR. code is as follows.

new Fb().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
new Twitter().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
...

mAdapter = new MyAdapter(getActivity(),
                (ArrayList<Model>) showList);
mListView.setAdapter(mAdapter);

I want the calls to be in parallel to save the time of fetching data. Now the idea is that i want all the four threads to complete the work and after that I want to display the sorted data as per timestamp. Right now the problem is that my setAdapter get called before the four threads complete fetching data as adapter is set on the UI thread. I want a mechanism to block the UI thread until all four threads complete fetching the data.

I have found that maybe I can use the shutDown() and awaitTermination() methods of ExecutorService . It would be great if anyone can help me in anyway. Thanks a lot

Upvotes: 0

Views: 321

Answers (4)

Sagar
Sagar

Reputation: 15

I could successfully handle the problem without going deeo into threading. I used the simple concept of making booleans true after execution of each thread. And in the UI thread I checked for infinte time i.e

while(true)
{
    if(flag1 && flag2 && flag3 && flag4)
    {
       //set the adapter and make the flags false 
       break;
    }

}

Dont forget to make the falgs false otherwise execution will go on for indefinite time.

Upvotes: 0

Mark
Mark

Reputation: 5566

What I would do is:

  1. Create an empty ArrayList in your activity
  2. Pass this arrayList to your adapter and set the adapter on your ListView. setAdapter method will not affect the listView - it will still be empty becase the list is empty.
  3. Start 4 AsyncTasks and fetch data. In onPostExecute() add this data to the ArrayList and invoke notifyDatasetChanged() on the adapter.

You should never block an UI thread because it is just wrong. When you want all data to be shown at once you can make a simple counter, make counter++ in every onPostExecute and invoke notifyDatasetChange() only when counter is 4.

Upvotes: 1

benjammin
benjammin

Reputation: 537

If you're really okay with blocking the UI thread, try this:

new Fb().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
new Twitter().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
...

AsyncTask.THREAD_POOL_EXECUTOR.shutdown();

while (!AsyncTask.THREAD_POOL_EXECUTOR.isTerminated()) {}

mAdapter = new MyAdapter(getActivity(),
                (ArrayList<Model>) showList);
mListView.setAdapter(mAdapter);

In most cases though, blocking the UI thread isn't a good idea.

Upvotes: 0

m-szalik
m-szalik

Reputation: 3654

Invoke method shutdown() from ExecutorService.

Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.

http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html

Upvotes: 0

Related Questions