Ahmed Khairy
Ahmed Khairy

Reputation: 145

Thread.join is not working

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!

Upvotes: 0

Views: 149

Answers (1)

xingbin
xingbin

Reputation: 28289

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.

Upvotes: 1

Related Questions