package com.mytest.formiaomiao;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
public abstract class MultiThreadService<T, R> {
public List<T> process(List<R> resources, int threadNumber) {
ExecutorService executorService = Executors.newFixedThreadPool(threadNumber);
List<Future<T>> futures = new ArrayList<>();
for (R resource : resources) {
SubTask subTask = new SubTask(resource);
futures.add(executorService.submit(subTask));
}
List<T> resultList = new ArrayList<>();
try {
for (Future<T> future : futures) {
if (future.get() != null) {
resultList.add(future.get());
}
}
} catch (InterruptedException | ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
executorService.shutdown();
}
return resultList;
}
protected abstract T doSubTask(R resource);
private class SubTask implements Callable<T> {
private R resource;
public SubTask(R resource) {
this.resource = resource;
}
@Override
public T call() throws Exception {
return doSubTask(resource);
}
}
}
package com.mytest.formiaomiao;
import java.sql.Connection;
import java.sql.Statement;
import java.util.Map;
public class MyMultiThreadService extends MultiThreadService<String, Map<Integer, String>> {
@Override
protected String doSubTask(Map<Integer, String> resource) {
String result = "OK";
try(Connection conn = MyUtils.getConnection(); Statement stat = conn.createStatement()) {
StringBuilder sb = new StringBuilder("insert formiaomiao.student (student_id, student_name) values ");
resource.forEach((k, v) -> {
sb.append(String.format("(%d, '%s')", k, v)).append(",");
});
String sql = sb.substring(0, sb.length() - 1);
stat.execute(sql);
} catch (Exception e) {
result = "NG";
}
return result;
}
}
package com.mytest.formiaomiao;
import org.apache.commons.lang3.RandomStringUtils;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.Future;
public class MyTest {
public static void main(String[] args) {
System.out.println("[1] " + LocalDateTime.now());
List<Map<Integer, String>> studentMapList = createListMap();
System.out.println(studentMapList.size());
System.out.println("[2] " + LocalDateTime.now());
MyMultiThreadService myThreadService = new MyMultiThreadService();
List<String> results = myThreadService.process(studentMapList, 5);
System.out.println("[3] " + LocalDateTime.now());
}
private static List<Map<Integer, String>> createListMap() {
List<Map<Integer, String>> studentMapList = new ArrayList<>();
Map<Integer, String> paramMap = new TreeMap<>();
for (int count = 1; count <= 1000 * 1000; count++) {
paramMap.put(count, RandomStringUtils.randomAlphabetic(6));
if (count % 100000 == 0) {
studentMapList.add(paramMap);
paramMap = new TreeMap<>();
}
}
return studentMapList;
}
}
package com.mytest.formiaomiao;
import com.zaxxer.hikari.HikariDataSource;
import java.sql.Connection;
import java.sql.SQLException;
public class MyUtils {
public static Connection getConnection() throws SQLException {
HikariDataSource ds = new HikariDataSource();
ds.setJdbcUrl("jdbc:mysql://localhost:3306/formiaomiao?characterEncoding=utf-8&serverTimezone=UTC");
ds.setUsername("root");
ds.setPassword("20546737");
return ds.getConnection();
}
}