Java 多线程批量处理数据

1 需求
在项目开发中需要处理100万多的数据,这些数据需要从mysql数据库中读取出来,再通过调用其他平台的接口推送数据。由于时间紧迫,数据需要在短时间内完成推送,采用单线程推送很慢,所以采用多线程推送来提高效率。

2 配置多线程
2.1 application.yml

thread-pool:
  core-pool-size: 4
  max-pool-size: 16
  queue-capacity: 80
  keep-alive-seconds: 120

2.2 创建ThreadPoolProperties

import lombok.Data;
import org.springframework.stereotype.Component;
import org.springframework.boot.context.properties.ConfigurationProperties;

@Data
@Component
@ConfigurationProperties(prefix = "thread-pool")
public class ThreadPoolProperties {
    /**
     * 线程池创建时候初始化的线程数
     */
    private int corePoolSize;

    /**
     * 线程池最大的线程数,只有在缓冲队列满了之后才会申请超过核心线程数的线程
     */
    private int maxPoolSize;

    /**
     * 用来缓冲执行任务的队列
     */
    private int queueCapacity;

    /**
     * 允许线程的空闲时间:当超过了核心线程出之外的线程在空闲时间到达之后会被销毁
     */
    private int keepAliveSeconds;
}

2.3 创建ThreadPoolConfig

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@EnableAsync
@Configuration
public class ThreadPoolConfig {
    private final ThreadPoolProperties threadPoolProperties;

    @Autowired
    public ThreadPoolConfig(ThreadPoolProperties threadPoolProperties) {
        this.threadPoolProperties = threadPoolProperties;
    }

    @Bean(name = "threadPoolTaskExecutor")
    public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(threadPoolProperties.getCorePoolSize());
        executor.setMaxPoolSize(threadPoolProperties.getMaxPoolSize());
        executor.setQueueCapacity(threadPoolProperties.getQueueCapacity());
        executor.setKeepAliveSeconds(threadPoolProperties.getKeepAliveSeconds());
        executor.setThreadNamePrefix("thread-pool-");
        return executor;
    }
}

3 多线程批量数据处理

public RequestResult multiThreadPush() {
    List<HistoryStudent> historyStudentList = historyStudentMapper.getList(0, 65867);
    // 分割集合
    List<List<HistoryStudent>> partitionData = partitionData(historyStudentList, 4);

    ThreadPoolTaskExecutor executor = SpringUtil.getBean("threadPoolTaskExecutor", ThreadPoolTaskExecutor.class);
  	// 计数器
    CountDownLatch latch = new CountDownLatch(partitionData.size());

    for (List<HistoryStudent> historyStudents : partitionData) {
        executor.execute(() -> {
            try {
                for (HistoryStudent historyStudent : historyStudents) {
                	// 单个数据处理
                    //processSingleData(historyStudent);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                latch.countDown();
            }
        });
    }

    try {
        latch.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    return RequestResult.success();
}
private List<List<HistoryStudent>> partitionData(List<HistoryStudent> dataList, int partitionSize) {
    List<List<HistoryStudent>> partitions = new ArrayList<>();

    int size = dataList.size();
    int batchSize = size / partitionSize;

    for (int i = 0; i < partitionSize; i++) {
        int fromIndex = i * batchSize;
        int toIndex = (i == partitionSize - 1) ? size : fromIndex + batchSize;

        partitions.add(dataList.subList(fromIndex, toIndex));
    }

    return partitions;
}

4 参考博客
Java多线程批量处理、线程池的使用
Java多线程处理大批量数据
java多线程批量处理数据

  • 1
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java多线程批量修改数据的示例代码: ```java import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class BatchUpdateDemo { private static final String URL = "jdbc:mysql://localhost:3306/test"; private static final String USER = "root"; private static final String PASSWORD = "123456"; public static void main(String[] args) { Connection conn = null; PreparedStatement pstmt = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(URL, USER, PASSWORD); conn.setAutoCommit(false); String sql = "update user set age = ? where id = ?"; pstmt = conn.prepareStatement(sql); // 模拟需要修改的数据 int[] ids = {1, 2, 3, 4, 5}; int[] ages = {20,21, 22, 23, 24}; // 创建线程池 ExecutorService executorService = Executors.newFixedThreadPool(5); // 批量修改数据 for (int i = 0; i < ids.length; i++) { int id = ids[i]; int age = ages[i]; executorService.execute(() -> { try { pstmt.setInt(1, age); pstmt.setInt(2, id); pstmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } }); } // 关闭线程池 executorService.shutdown(); while (!executorService.isTerminated()) { Thread.sleep(100); } // 提交事务 conn.commit(); } catch (ClassNotFoundException | SQLException | InterruptedException e) { e.printStackTrace(); try { if (conn != null) { conn.rollback(); } } catch (SQLException ex) { ex.printStackTrace(); } } finally { try { if (pstmt != null) { pstmt.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } } } ``` 该示例代码使用了线程池来批量修改数据,通过创建一个固定大小的线程池,将每个修改操作封装成一个任务,交给线程池去执行。这样可以避免频繁地创建和销毁线程,提高了程序的性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值