Query Database Using Multithreading in for Loop inside

  java, question

Now it is necessary to query the database in a loop and then summarize the data. Do you have any examples and suggestions for reference

In fact, if you want to use this method because each query task is relatively slow, it is better to optimize sql. Or you can use the following thread pool method to handle it, but the complexity of the code will be greatly increased.
The data type of the wrapper returned by Futrue corresponds to the type returned by your sql.
Or you can use fork/join to handle it.

public class CallableAndFuture {
 
 public static void main(String[] args) {
 ExecutorService threadPool = Executors.newSingleThreadExecutor();
 List<Future<Integer>> futures = new ArrayList<>();
 for (int i = 10;   i < 15;  I plus)
 futures.add(threadPool.submit(new Task(i)));
 bracket
 try {
 Thread.sleep(1000);  //Maybe do something
 
 int allSum = 0;
 for (Future<Integer> f : futures) {
 int fsum = f.get();
 Out.println ("sum:" plus fsum ");
 AllSum plus = fsum;
 bracket
 Out.println ("allSum:" plus allsum ");
 } catch (InterruptedException e) {
 e.printStackTrace();
 } catch (ExecutionException e) {
 e.printStackTrace();
 bracket
 threadPool.shutdown();
 bracket
 bracket
 
 
 class Task implements Callable<Integer> {
 private int i;
 
 public Task(int i) {
 this.i = i;
 bracket
 
 @Override
 public Integer call() throws Exception {
 //Replace with db query
 int sum = 0;
 for (int j = 0;   j <= i;  J plus)
 Sum plus = j;
 bracket
 return sum;
 bracket
 
 bracket