java实现多线程分页插入数据
时间: 2023-07-29 16:06:15 浏览: 144
下面是Java实现多线程分页插入数据的示例代码:
```java
import java.sql.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class MultiThreadInsert {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/test";
String username = "root";
String password = "123456";
ExecutorService executorService = Executors.newFixedThreadPool(10);
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(url, username, password);
String sql = "INSERT INTO user(name, age) VALUES(?, ?)";
PreparedStatement ps = conn.prepareStatement(sql);
int pageSize = 1000; //每页大小
int total = 1000000; //总数据量
int totalPages = total / pageSize; //总页数
for (int i = 0; i < totalPages; i++){
int currentPage = i + 1;
int start = i * pageSize;
int end = (i + 1) * pageSize;
executorService.execute(new InsertDataThread(ps, start, end));
}
executorService.shutdown();
}catch (Exception e){
e.printStackTrace();
}
}
static class InsertDataThread implements Runnable {
private PreparedStatement ps;
private int start;
private int end;
public InsertDataThread(PreparedStatement ps, int start, int end){
this.ps = ps;
this.start = start;
this.end = end;
}
public void run(){
try{
for (int i = start; i < end; i++){
ps.setString(1, "user" + i);
ps.setInt(2, i % 100);
ps.addBatch();
if (i % 1000 == 0){
ps.executeBatch();
ps.clearBatch();
}
}
}catch (Exception e){
e.printStackTrace();
}
}
}
}
```
代码中使用了Java的线程池ExecutorService来管理线程,通过构造函数传入PreparedStatement对象和起始位置、结束位置,然后在run()方法中执行批量插入操作。每1000条数据执行一次批量插入操作。可以通过修改pageSize和total参数来修改每页大小和总数据量。
阅读全文