使用多线程往List添加数据

我们在日常写代码的过程中,经常会使用多线程提高效率,我们在使用多线程过程中难免会出现往List集合修改数据。
下面我们来尝试一下往ArrayList 添加数据:

public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        for (int i = 10000000; i >= 1; i--) {
            list.add(0);
        }
        System.out.println("源集合数量:"+list.size());
        List<Integer> newList = new ArrayList<>();
        long start = System.currentTimeMillis();

        ExecutorService executor = Executors.newFixedThreadPool(100);
        for (Integer integer : list) {
            executor.submit(()->{
                newList.add(integer+1);
            });
        }
        executor.shutdown();
        try {
            executor.awaitTermination(6, TimeUnit.MINUTES);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("时间:"+(end-start)+"ms");

        System.out.println("新集合数量:"+newList.size());

我们使用线程池给 ArrayList 添加一千万个元素。来看下结果:
在这里插入图片描述

会发现新List’的数据会少于一千万,这是为什么呢?

因为 ArrayList 不是线程安全的,在高并发情况下对list进行数据添加会出现数据丢失的情况。
并且一个线程在遍历List,另一个线程修改List,会报ConcurrentModificationException(并发修改异常)错误

那么如果我们确实需要 并发对数据进行操作,并且对结果进行收集处理,应该怎么做呢?
一,使用Vector
在这里插入图片描述
从源码介绍里面我们可以看出 Viector是线程安全的,但后面也说明了,如果对线程安全没有要求,建议使用ArrayList,因为ArrayList单分效率更高。
从源码里面可以看到:

/**
     * Sets the size of this vector. If the new size is greater than the
     * current size, new {@code null} items are added to the end of
     * the vector. If the new size is less than the current size, all
     * components at index {@code newSize} and greater are discarded.
     *
     * @param  newSize   the new size of this vector
     * @throws ArrayIndexOutOfBoundsException if the new size is negative
     */
    public synchronized void setSize(int newSize) {
        modCount++;
        if (newSize > elementCount) {
            ensureCapacityHelper(newSize);
        } else {
            for (int i = newSize ; i < elementCount ; i++) {
                elementData[i] = null;
            }
        }
        elementCount = newSize;
    }

    /**
     * Returns the current capacity of this vector.
     *
     * @return  the current capacity (the length of its internal
     *          data array, kept in the field {@code elementData}
     *          of this vector)
     */
    public synchronized int capacity() {
        return elementData.length;
    }

    /**
     * Returns the number of components in this vector.
     *
     * @return  the number of components in this vector
     */
    public synchronized int size() {
        return elementCount;
    }

    /**
     * Tests if this vector has no components.
     *
     * @return  {@code true} if and only if this vector has
     *          no components, that is, its size is zero;
     *          {@code false} otherwise.
     */
    public synchronized boolean isEmpty() {
        return elementCount == 0;
    }

Vector里面的操作方法,都加上了synchronized 关键字。下面来使用Vector走一遍代码:

public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        for (int i = 10000000; i >= 1; i--) {
            list.add(0);
        }
        System.out.println("源集合数量:"+list.size());
        List<Integer> newVector = new Vector<>();
        long start = System.currentTimeMillis();

        ExecutorService executor = Executors.newFixedThreadPool(100);
        for (Integer integer : list) {
            executor.submit(()->{
                newVector.add(integer+1);
            });
        }
        executor.shutdown();
        try {
            executor.awaitTermination(6, TimeUnit.MINUTES);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("时间:"+(end-start)+"ms");

        System.out.println("newVector数量:"+newVector.size());
    }

看下结果:
在这里插入图片描述
我们可以发现现在,新Vector里面的数量正好是一千万个。但是时间上要长于ArrayList。

二、使用Collections.synchronizedList()进行包装

public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        for (int i = 10000000; i >= 1; i--) {
            list.add(0);
        }
        System.out.println("源集合数量:"+list.size());
        /**
         * Collections.synchronizedList()包装
         */
        List<Integer> newCollList = Collections.synchronizedList(new ArrayList<>());
        long start = System.currentTimeMillis();

        ExecutorService executor = Executors.newFixedThreadPool(100);
        for (Integer integer : list) {
            executor.submit(()->{
                newCollList.add(integer+1);
            });
        }
        executor.shutdown();
        try {
            executor.awaitTermination(6, TimeUnit.MINUTES);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("时间:"+(end-start)+"ms");

        System.out.println("newCollList新集合数量:"+newCollList.size());
    }

结果:
在这里插入图片描述
我们可以发现也是一千万条。时间上和Vector差距不大,因给给ArrayList进行了包装以后等于是给ArrayList里面所有的方法都加上了 synchronized,和Vector实现效果差不多。

总结:在并发给List进行修改时,可以使用Vector或者Collections.synchronizedList(),不要直接使用ArrayList,在非并发情况下尽量使用ArrayList;

好的,这个问题我可以帮你回答。以下是 Java 实现使用线程池多线程分页插入数据的代码示例: ```java import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class MultiThreadInsert { private static final int BATCH_SIZE = 500; private static final int THREAD_POOL_SIZE = 10; public void insert(List<Data> dataList) { ExecutorService executorService = Executors.newFixedThreadPool(THREAD_POOL_SIZE); int totalSize = dataList.size(); int totalPages = (int) Math.ceil((double) totalSize / BATCH_SIZE); for (int i = 0; i < totalPages; i++) { int startIndex = i * BATCH_SIZE; int endIndex = Math.min((i + 1) * BATCH_SIZE, totalSize); List<Data> subList = dataList.subList(startIndex, endIndex); executorService.execute(new InsertTask(subList)); } executorService.shutdown(); } private static class InsertTask implements Runnable { private List<Data> dataList; public InsertTask(List<Data> dataList) { this.dataList = dataList; } @Override public void run() { Connection connection = null; PreparedStatement preparedStatement = null; try { connection = getConnection(); // 获取数据库连接 connection.setAutoCommit(false); // 关闭自动提交 String sql = "INSERT INTO table_name (column1, column2, ...) VALUES (?, ?, ...)"; preparedStatement = connection.prepareStatement(sql); for (Data data : dataList) { preparedStatement.setString(1, data.getColumn1()); preparedStatement.setInt(2, data.getColumn2()); // 继续设置 preparedStatement 的参数 preparedStatement.addBatch(); } preparedStatement.executeBatch(); connection.commit(); // 提交事务 } catch (SQLException e) { // 处理异常 } finally { close(preparedStatement); close(connection); } } private Connection getConnection() throws SQLException { // 获取数据库连接 } private void close(Connection connection) { // 关闭数据库连接 } private void close(PreparedStatement preparedStatement) { // 关闭 PreparedStatement } } private static class Data { private String column1; private int column2; // 其他属性及 setter、getter 方法 } } ``` 其中,`MultiThreadInsert` 类中的 `insert` 方法接收一个 `List<Data>` 参数,表示需要插入的数据列表。该方法将数据按照 BATCH_SIZE(每批次插入的数据量) 进行分页,然后使用固定大小的线程池(大小为 THREAD_POOL_SIZE)执行插入操作。 `InsertTask` 类表示每个插入任务,其中的 `run` 方法会在新的线程中执行。在 `run` 方法中,首先获取数据库连接并关闭自动提交,然后根据每个数据对象设置 `PreparedStatement` 的参数,并将插入语句添加到批处理中,最后执行批处理并提交事务。如果执行过程中发生异常,需要进行异常处理并关闭数据库连接和 `PreparedStatement`。 需要注意的是,在插入操作中应该尽量避免使用锁,因为锁会降低多线程并发执行的效率。另外,数据的插入顺序可能会被打乱,所以如果需要保证数据的顺序,需要在查询时进行排序。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值