Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 276669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:57:27+00:00 2026-05-12T00:57:27+00:00

I have a method that takes an array of queries, and I need to

  • 0

I have a method that takes an array of queries, and I need to run them against different search engine Web API’s, such as Google’s or Yahoo’s. In order to parallelize the process, a thread is spawned for each query, which are then joined at the end, since my application can only continue after I have the results of every query. I currently have something along these lines:

public abstract class class Query extends Thread {
    private String query;

    public abstract Result[] querySearchEngine();
    @Override
    public void run() {
        Result[] results = querySearchEngine(query);
        Querier.addResults(results);
    }

}

public class GoogleQuery extends Query {
    public Result querySearchEngine(String query) { 
        // access google rest API
    }
}

public class Querier {
    /* Every class that implements Query fills this array */
    private static ArrayList<Result> aggregatedResults;

    public static void addResults(Result[]) { // add to aggregatedResults }

    public static Result[] queryAll(Query[] queries) {
        /* for each thread, start it, to aggregate results */
        for (Query query : queries) {
            query.start();
        }
        for (Query query : queries) {
            query.join();
        }
        return aggregatedResults;
    }
}

Recently, I have found that there’s a new API in Java for doing concurrent jobs. Namely, the Callable interface, FutureTask and ExecutorService. I was wondering if this new API is the one that should be used, and if they are more efficient than the traditional ones, Runnable and Thread.

After studying this new API, I came up with the following code (simplified version):

   public abstract class Query implements Callable<Result[]> {
        private final String query; // gets set in the constructor

        public abstract Result[] querySearchEngine();
        @Override
        public Result[] call() {
            return querySearchEngine(query);
        }
    }

public class Querier {   
        private ArrayList<Result> aggregatedResults;

        public Result[] queryAll(Query[] queries) {
            List<Future<Result[]>> futures = new ArrayList<Future<Result[]>>(queries.length);
            final ExecutorService service = Executors.newFixedThreadPool(queries.length);  
            for (Query query : queries) {
                futures.add(service.submit(query));  
            }
            for (Future<Result[]> future : futures) {  
                aggregatedResults.add(future.get());  // get() is somewhat similar to join?
            }  
            return aggregatedResults;
        }
    }

I’m new to this concurrency API, and I’d like to know if there’s something that can be improved in the above code, and if it’s better than the first option (using Thread). There are some classes which I didn’t explore, such as FutureTask, et cetera. I’d love to hear any advice on that as well.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T00:57:27+00:00Added an answer on May 12, 2026 at 12:57 am

    Several problems with your code.

    1. You should probably be using the ExecutorService.invokeAll() method.
      The cost of creating new threads and a new thread pool can be significant (though maybe not compared to calling external search engines). invokeAll() can manage the threads for you.
    2. You probably don’t want to mix arrays and generics.
    3. You are calling aggregatedResults.add() instead of addAll().
    4. You don’t need to use member variables when they could be local to the queryAll() function call.

    So, something like the following should work:

    public abstract class Query implements Callable<List<Result>> {
        private final String query; // gets set in the constructor
    
        public abstract List<Result> querySearchEngine();
        @Override
        public List<Result> call() {
            return querySearchEngine(query);
        }
    }
    
    public class Querier {   
        private static final ExecutorService executor = Executors.newCachedThreadPool();
    
        public List<Result> queryAll(List<Query> queries) {
            List<Future<List<Result>>> futures = executor.submitAll(queries);
            List<Result> aggregatedResults = new ArrayList<Result>();
            for (Future<List<Result>> future : futures) {  
                aggregatedResults.addAll(future.get());  // get() is somewhat similar to join?
            }  
            return aggregatedResults;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method that i want to mock that takes an array as
Let's say I have a method m() that takes an array of Strings as
I have a method in C++ that takes an array of doubles as an
I have a method that takes an IQueryable. Is there a LINQ query that
Suppose I have a method that takes an object of some kind as an
I have an init method that takes an (id) argument: -(id) initWithObject:(id) obj; I'm
So, I have a method that performs a parametrised LIKE query. The method takes
I have a method that needs to accept an array of country names, and
I have an method that takes in an observable collection (returned from a webservice)
I have a problem with my database class. I have a method that takes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.