0

I want to execute the following threads, and wait for them to finish in order to continue working, However, this is not achieved through my code and I don't know why, I suppose join should force the current thread to wait for all threads to finish!. I would be pleased if anyone could help me!

Here's my code:

ArrayList<Thread> retrievalThreads=new ArrayList<Thread>();

    retrievalThreads.add(new Thread(new Runnable() {
        @Override
        public void run() {
            getProfilePictures(false);
        }
    }));

    retrievalThreads.add(new Thread(new Runnable() {
        @Override
        public void run() {
            getNumVotesThisWeek();
        }
    }));

    retrievalThreads.add( new Thread(new Runnable() {
        @Override
        public void run() {
            getTotalVotes(true);
        }
    }));

    retrievalThreads.add(new Thread(new Runnable() {
        @Override
        public void run() {
            getTotalMatches();
        }
    }));

    retrievalThreads.add(new Thread(new Runnable() {
        @Override
        public void run() {
            getUserActivityLevel();
        }
    }));

    retrievalThreads.add(new Thread(new Runnable() {
        @Override
        public void run() {
            getUserMatchMakerLevel();
        }
    }));

    retrievalThreads.add(new Thread(new Runnable() {
        @Override
        public void run() {
            getLastBoostTimeStamp(true);
        }
    }));

    retrievalThreads.add(new Thread(new Runnable() {
        @Override
        public void run() {
            getLastStealTimeStamp(true);
        }
    }));

    retrievalThreads.add(new Thread(new Runnable() {
        @Override
        public void run() {
            getFacebookFriends();
        }
    }));

    //Start all above threads
    for (int i=0;i<retrievalThreads.size();i++)
        retrievalThreads.get(i).start();

    //Wait for them to finish
    for (int i=0;i<retrievalThreads.size();i++){
        try {
            retrievalThreads.get(i).join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    Log.i("Finished","TheAbove");

The printing in the Log Console is done before threads finish execution!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Ahmed Khairy
  • 145
  • 2
  • 16
  • Yes, calling `join` will force the current thread to wait for all threads in `retrievalThreads` finish. And the code you posted is fine. Can you post more details? The "Log Console is done" before which method? – xingbin Feb 24 '18 at 16:36
  • Printing is done before all threads finish @user6690200 , as if I didn't call join() – Ahmed Khairy Feb 24 '18 at 16:45
  • I understand. But the code you post seems ok. Can you post more details? – xingbin Feb 24 '18 at 16:58
  • details like what ? :D – Ahmed Khairy Feb 24 '18 at 17:02
  • How to create a Minimal, Complete, and Verifiable example? https://stackoverflow.com/help/mcve – xingbin Feb 24 '18 at 17:05
  • @user6690200 the methods I'm executing inside the run() method are volley requests – Ahmed Khairy Feb 24 '18 at 17:24
  • The code you have provided here looks fine. If you put a log message in each of your threads at the end of their run methods, you will see that join is working just fine. – Gray Feb 26 '18 at 00:22

1 Answers1

1

Thread.join() will wait only for the thread which creates volley reuqest, not all threads. Volley will spawn other threads to send the request asynchronously. You need synchronous volley requests.

xingbin
  • 27,410
  • 9
  • 53
  • 103