Java uses thread pool to insert or update data in batches

Keywords: PHP MySQL Database Oracle Java

Requirements: when developing business reports, it is necessary to read data from MySQL database, then write it to the database, and run batches using scheduled tasks.

Analysis: ① considering performance; ② MySQL is not as convenient and powerful as Oracle. To sum up, thread pool is used to insert MySQL database in batch Submission Scheme. This scheme also supports update operation and paging operation on Java side. The code is as follows:

 1     private void batchDeal(List data, int batchNum) throws InterruptedException {
 2         int totalNum = data.size();
 3         int pageNum = totalNum % batchNum == 0 ? totalNum / batchNum : totalNum / batchNum + 1;
 4         ThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(pageNum);
 5         try {
 6             CountDownLatch countDownLatch = new CountDownLatch(pageNum);
 7             List subData = null;
 8             int fromIndex, toIndex;
 9             for (int i = 0; i < pageNum; i++) {
10                 fromIndex = i * batchNum;
11                 toIndex = Math.min(totalNum, fromIndex + batchNum);
12                 subData = data.subList(fromIndex, toIndex);
13                 ImportTask task = new ImportTask(subData, countDownLatch);
14                 executor.execute(task);
15             }
16             // The main thread must call immediately after starting other threads CountDownLatch.await()Method,
17             // In this way, the operation of the main thread will block on this method until other threads complete their respective tasks.
18             // When the counter value is equal to 0, the main thread can pass await()Method to resume performing its own tasks.
19             countDownLatch.await();
20             logger.info("Data operation completed!You can start other business here");
21         } finally {
22             // Close thread pool, release resources
23             executor.shutdown();
24         }
25     }
26 
27     class ImportTask implements Runnable {
28         private List list;
29         private CountDownLatch countDownLatch;
30 
31         public ImportTask(List data, CountDownLatch countDownLatch) {
32             this.list = data;
33             this.countDownLatch = countDownLatch;
34         }
35 
36         @Override
37         public void run() {
38             if (null != list) {
39                 // Business logic, such as batch insert perhaps update
40                 logger.info("Now the data of the operation is{}", list);
41             }
42             // Signal completion of thread task
43             countDownLatch.countDown();
44         }
45     }

In line 1, List data represents the incoming data, and batchNum represents the number of each processing, such as 500 pieces.

Posted by Kasuke_Akira on Sat, 02 Nov 2019 04:34:20 -0700