java代碼多線程批量插入數據


package root.report.control.dict;

import org.apache.ibatis.session.SqlSession;
import root.report.db.DbFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Auther: pccw
 * @Date: 2018/10/29 17:45
 * @Description:
 */
/*

往自己本地mysql 當中插入10W條記錄

 */
public class TestTwo {

    public static void main(String args[]) throws SQLException {
        SqlSession sqlSession = DbFactory.Open(DbFactory.FORM);
        // insert(sqlSession);
        insertTwo(sqlSession);
    }
 // 多線程案例  使用fix線程  規定為5個
    public static void insertTwo(SqlSession sqlSession) throws SQLException {

        Connection conn = sqlSession.getConnection();
        // 開始時間
        Long begin = new Date().getTime();


        final StringBuffer suffix = new StringBuffer();
        // sql前綴
        String prefix = "INSERT INTO test_dict (code,name) VALUES ";
        // 設置事務為非自動提交
        conn.setAutoCommit(false);
        // 比起st,pst會更好些
        PreparedStatement pst = (PreparedStatement) conn.prepareStatement("");//准備執行語句

        final CountDownLatch count = new CountDownLatch(5); //相當線程執行計時器,await()讓線程等待,用countDown()消初始化數量。當數量等於0時線程喚醒
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(5); //線程池
        synchronized (fixedThreadPool) {
            for (int i = 1; i <= 5; i++) {
                final int index = i;
                if (i == 1) {
                    fixedThreadPool.execute(new Runnable() {
                        @Override
                        public void run() {
                            for (int j = 1; j < 10; j++) {
                                // 構建SQL后綴
                                suffix.append("(" + j + "," + "'0000" + j + "'),");
                            }
                            count.countDown();
                        }
                    });
                } else if (i == 2) {
                    fixedThreadPool.execute(new Runnable() {
                        @Override
                        public void run() {
                            for (int j = 10; j < 100; j++) {
                                // 構建SQL后綴
                                suffix.append("(" + j + "," + "'000" + j + "'),");
                            }
                            count.countDown();
                        }
                    });
                } else if (i == 3) {
                    fixedThreadPool.execute(new Runnable() {
                        @Override
                        public void run() {
                            for (int j = 100; j < 1000; j++) {
                                // 構建SQL后綴
                                suffix.append("(" + j + "," + "'00" + j + "'),");
                            }
                            count.countDown();
                        }
                    });
                } else if (i == 4) {
                    fixedThreadPool.execute(new Runnable() {
                        @Override
                        public void run() {
                            for (int j = 1000; j < 10000; j++) {
                                // 構建SQL后綴
                                suffix.append("(" + j + "," + "'0" + j + "'),");
                            }
                            count.countDown();
                        }
                    });
                } else {
                    fixedThreadPool.execute(new Runnable() {
                        @Override
                        public void run() {
                            for (int j = 10000; j <= 99999; j++) {
                                // 構建SQL后綴
                                suffix.append("(" + j + "," + "'" + j + "'),");
                            }
                            count.countDown();
                        }
                    });
                }
            }
        }
        try {
            count.await();
            // 構建完整SQL
            String sql = prefix + suffix.substring(0, suffix.length() - 1);

            // 添加執行SQL
            pst.addBatch(sql);
            // 執行操作
            pst.executeBatch();
            // 提交事務
            conn.commit();
            // 頭等連接
            pst.close();
            conn.close();
            // 結束時間
            Long end = new Date().getTime();

            System.out.println("10萬條數據插入花費時間 : " + (end - begin)  + " ms");
            System.out.println("插入完成");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            fixedThreadPool.shutdown();
        }
    }

    // 測試使用 fetch 按照指定規格讀取數據


}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM