简体   繁体   English

Android中的多线程

[英]Multi-threading in Android

I'm new to Android and Java. 我是Android和Java的新手。 I'm trying to download 1000 plus images. 我正在尝试下载1000多个图像。 I don't want to do that serially in a UI thread as that will be slow. 我不想在UI线程中串行执行此操作,因为那样会很慢。 Hence, I implemented multi-threading using thread and runnable in the below manner. 因此,我以下面的方式使用线程和可运行multi-threading实现了multi-threading

The for-loop will be called 1000 plus times. for循环将被调用1000次以上。 So is it an efficient way of achieving it? 那么这是实现这一目标的有效方法吗? Will the OS manage the thread pool by its own? OS会自己管理线程池吗?

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());
    }
}

为确保您无法在MainThread(UI线程)中执行此操作,因为如果执行了此操作,则应用程序将不会响应..然后它将被系统杀死,您可以使用AsyncTask类来执行所需的操作,但我更喜欢使用intentservice,但是您必须使用Intentservice,它是一个工作线程(长时间操作),但是请注意,在完成当前任务之前,intentservice不会执行任何操作,如果您需要并行下载它,则必须使用Service才能正常工作使用UI线程,因此您需要asyncTask才能执行操作,但要确保调用stopSelf()不同于intentService,一旦完成,它将停止

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 您可以使用AsyncTask参考: 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);

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. 我之前曾开发过一个电子商务应用程序,遇到过类似的问题,我必须为每个类别下载200多个图像,我这样做的方法是在AsyncTask中使用循环,每次下载完成后图像都是使用onProgessUpdate()函数显示在相关位置。我无法共享实际代码,因此我将给出一个框架示例。

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
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM