-1

How to make sure that the first iteration is run by One Thread and second iteration by Another Thread.

In the below code snippet, the runQuery method executes a Query and brings the resultSet as output.

I have two such queries and I want to run both in Parallel.

for (int i = 0; i < list.size(); i++) {
    String ProcessedRecord = list.get(i);
    String app_name = application.get(i);

    // ResultSet feedDetails = runQuery(ProcessedRecord,app_name); 
    try {
        System.out.println("Inside try block ... ");
        ResultSet feedDetails = dat.runQuery(ProcessedRecord, app_name);
        rs_feedDetails.add(feedDetails); 
    } catch(SQLException e) {
        e.printStackTrace();
    }
}
Sean Bright
  • 118,630
  • 17
  • 138
  • 146
user3254725
  • 65
  • 1
  • 9
  • 1
    You posted a very close question few hours ago too. http://stackoverflow.com/questions/37811698/how-to-connect-to-3-different-databases-and-run-queries-parallel-through-jdbc – Sneh Jun 14 '16 at 15:45

1 Answers1

1

You can do this with the help of ExecutorService, Callables and Futures.

Below is the modified code :

    ExecutorService pool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); //A thread pool to execute your tasks
    List<Future<ResultSet>> futures = new ArrayList<>(); //List to hold futures

    for (int i = 0; i < list.size(); i++) {
        String ProcessedRecord = list.get(i);
        String app_name = application.get(i);

        // ResultSet feedDetails = runQuery(ProcessedRecord,app_name); 
        try {
            Future<ResultSet> toAdd = pool.submit(new Callable<ResultSet>() { //submit a callable of resultset to the pool
                @Override
                public ResultSet call() throws Exception {
                    return dat.runQuery(ProcessedRecord, app_name);
                }
            });
            futures.add(toAdd);
        } catch(SQLException e) {
            e.printStackTrace();
        }
    }
    pool.shutdown(); // shut down the thread pool
    for(int i = 0; i < futures.size(); i++) {
        rs_feedDetails.add(futures.get(i).get()); //invoke get method of every future in your list. The get method will return you the result.
    }

EDIT

As mentioned in the comments, under certain circumstances tasks may be executed on the same thread in serial - it is possible to enforce parallelism with ExecutorService parameters like "parallelism" or "amountThreads"

specializt
  • 1,913
  • 15
  • 26
Sneh
  • 3,527
  • 2
  • 19
  • 37
  • I am being pedantic, but there is no guarantee that these tasks will be executed by two different threads. They may be executed in parallel by two different threads, or serially in a single thread. That being said, this is the correct answer to the question. – Sean Bright Jun 14 '16 at 15:54
  • Yeah, I should have pointed that out. Thanks @SeanBright – Sneh Jun 14 '16 at 15:55
  • um ... that would pretty much **defeat the purpose of ExecutorServices** - as long as `availableProcessors` (in your example) is greater than 1, submitted tasks will **always** be executed in parallel - except for the rare case of ultra-fast task completion -- if task A completes **before** task B is *submitted* (astronomically unlikely) then the two tasks **may** be executed one after another on the same `Thread`, but being hit by a metor is much more likely. – specializt Jun 14 '16 at 16:04