简体   繁体   中英

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!

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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