Manish
Manish

Reputation: 1326

Android Thread Synchronization

I'm trying to get bitmap from multiple image urls and adding it in arraylist, I'm using below code for this.

for (int j = 0; j <= imagePaths.size() - 1; j++) {
                  final String urltemp =  "http://SiteName/" + imagePaths.get(j);
                  new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                final URL url = new URL(urltemp);
                                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                                connection.setDoInput(true);
                                connection.connect();
                                InputStream input = null;
                                input = connection.getInputStream();
                                Bitmap myBitmap = BitmapFactory.decodeStream(input);
                                arrayList.add(myBitmap);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }).start();
                }

code is working fine, but image order in ArryList changes randomly. how should I synchronize threads inside for loop, so that images are added in the order of for loop.

Upvotes: 0

Views: 272

Answers (2)

drulabs
drulabs

Reputation: 3121

Let me start by saying, the code you have is very bad as there is no limit for thread count. This is happening because every download is happening in a separate thread and how long a thread runs depends on size of image to be downloaded.

But if you still want to go this way, use this code:

ArrayList<Bitmap> arrayList = new Arraylist<~>[imagePaths.size()]
for (int j = 0; j <= imagePaths.size() - 1; j++) {
              final String urltemp =  "http://SiteName/" + imagePaths.get(j);
              new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            final URL url = new URL(urltemp);
                            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                            connection.setDoInput(true);
                            connection.connect();
                            InputStream input = null;
                            input = connection.getInputStream();
                            Bitmap myBitmap = BitmapFactory.decodeStream(input);

                            //here is the change
                            arrayList.add(j, myBitmap);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }

Upvotes: 1

Methnani Bilel
Methnani Bilel

Reputation: 1385

1- You should use a thread pool for this kind of job 2- Don't synchronize your threads just put the bitmap in a container class and store its index then later sort the array.

Upvotes: 0

Related Questions