-1

I have a thread that is downloading images in the background and need it to finish before upload starts. I have a button that starts the uploading but not sure how to check if my first thread is done/ wait for it to be done.

here is my downloading thread:

 t = new Thread(new Runnable() {
    // NEW THREAD BECAUSE NETWORK REQUEST WILL BE MADE THAT WILL BE A LONG PROCESS & BLOCK UI
    // IF CALLED IN UI THREAD
    public void run() {
        try {
            for (int i = 0; i < Constants2photo.IMAGES.size(); i++) {

                Uri myUri = Uri.parse(Constants2photo.IMAGES.get(i).get("url"));
                String fileLocation = loadPicasaImageFromGallery(myUri);
                Constants2photo.IMAGES.get(i).put("fileLocation", fileLocation);
                System.out.println("fileloc: " + fileLocation);

            }
            System.out.println("done getting files - " + Constants2photo.IMAGES);

            //this part would download the image to the media store//TODO add this to a background task so sending event is faster.
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
});
t.start();

threads.add(t); //this is a List

The above code is just in my onactivityresult method in my class

My button code with thread #2 that needs to start after t is done:

public void submitPhotos(View view){
 //convert and submit photos here

    Thread t2 = new Thread(new Runnable() {
        // NEW THREAD BECAUSE NETWORK REQUEST WILL BE MADE THAT WILL BE A LONG PROCESS & BLOCK UI
        // IF CALLED IN UI THREAD
        public void run() {
            try {

                for (int i = 0; i < Constants2photo.IMAGES.size(); i++) {


                  ...
                    System.out.println("encoded string: " + encodedString);

                }

            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    });
    t2.start();

So where do i use join to make t2 execute only when t is done??

BluGeni
  • 3,378
  • 8
  • 36
  • 64
  • `Thread#join()`. Anyway if you want to wait for a single thread to finish, then maybe you don't want to spawn a new thread... In your case I suggest to spawn a thread for each image instead of one for all, this way you can download them on parallel. – m0skit0 Jan 14 '15 at 20:00
  • @m0skit0 Well I dont want the above thread on the main thread. Can you be a little more specific about `Thread#join()` – BluGeni Jan 14 '15 at 20:02
  • http://stackoverflow.com/questions/1908515/java-how-to-use-thread-join – m0skit0 Jan 14 '15 at 20:02
  • @m0skit0 Ive added and edit my code but not sure where to do the join, can you help? – BluGeni Jan 14 '15 at 20:52
  • Did you even read the link I gave you? `t.start(); t.join(); t2.start();`. – m0skit0 Jan 14 '15 at 21:10
  • @m0skit0 I did and tried it but doing that blocks the main thread, which i dont want to do. – BluGeni Jan 14 '15 at 21:14
  • Then you have to pass `t` reference to `t2` and join there. – m0skit0 Jan 14 '15 at 21:15

1 Answers1

0

Thread.join() is the proper solution as m0skit0 mentions. Ignore his/her comment about not spawning a thread. If you want to keep the UI responsive for other purposes and you just want to activate the upload button after this thread completes, then join() is your solution.

Sensei
  • 124
  • 5
  • I am a little confused on how it is suppose to work. Can you give me another example. I dont understand the linked example by m0skit0 – BluGeni Jan 14 '15 at 20:28