rsd_unleashed
rsd_unleashed

Reputation: 161

Multi-threading in Android

I'm new to Android and Java. I'm trying to download 1000 plus images. I don't want to do that serially in a UI thread as that will be slow. Hence, I implemented multi-threading using thread and runnable in the below manner.

The for-loop will be called 1000 plus times. So is it an efficient way of achieving it? Will the OS manage the thread pool by its own?

private void syncS3Data() {
    tStart = System.currentTimeMillis();
    try {
        for (final AWSSyncFile f : awsSyncData.getFiles()) {
            new Thread(new Runnable() {

                @Override
                public void run() {
                    beginDownload(f);
                }

            }).start();
        }
    } catch (Exception ex) {
        progressDialog.dismiss();
        showMessage("Error:" + ex.getStackTrace().toString());
    }
}

Upvotes: 0

Views: 273

Answers (3)

Vinay George Roy
Vinay George Roy

Reputation: 61

I had developed an e-commerce app before and have encountered a similar problem in which I had to download some 200+ images for each category.The way I did it was using a loop within an AsyncTask and after each download was completed the image was displayed at the relevant place using the onProgessUpdate() function.I can't share the actual code,so i will give a skeleton example.

public class DownloadImages extends AsyncTask<String,String,String>
{
  File image;
  protected String doInBackground(String... params)
    {
      //download the image here and lets say its stored in the variable file
      //call publishProgress() to run onProgressUpdate()


    }
  protected void onProgressUpdate(String... values)
  {
     //use the image in variable file to update the UI
  }
}

Upvotes: 1

dpaksoni
dpaksoni

Reputation: 327

Instead of creating threads for each download, create one thread and use that for downloading all images.

You can use AsyncTask Refer: https://developer.android.com/reference/android/os/AsyncTask.html

private class DownloadFilesTask extends AsyncTask<SomeObject, Integer, Long> {
    protected Long doInBackground(SomeObject... objs) {

        for (final AWSSyncFile f : obj.getFiles()) {
           beginDownload(f);
        }
    }

    protected void onPostExecute(Long result) {
       //Task Completed
    }

new DownloadFilesTask().execute(someObj);

Upvotes: 1

Basil Battikhi
Basil Battikhi

Reputation: 2668

for Sure you can't do that in MainThread (UI Thread) because if you did, the application will not be responding.. and then it will be killed by system, you can use AsyncTask class in order to do what do you need but i prefer to use intentservice However you have to use Intentservice it's a worker thread (long operation) but be noted intentservice will not execute anything until it finish the current task, if you need to download it in parallel then you have to use Service it works with UI Thread so you need asyncTask in order to perform the action but make sure of calling stopSelf() unlike intentService it will be stopped once it finish

Upvotes: 2

Related Questions