PostGrepSql 线程池多线程数据插入

需求

数据库表复制,数据量大时,用单纯的客户端工具只能单线程导入,时间耗时长。本文主要采用 JDBC线程池 解决这个问题。

JDBC

我用的 PostGrepSql 数据库,根据自己的数据库 添加pom.xml

 <dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.1-901-1.jdbc4</version>
 </dependency>
工具类
static String url = "*";
static String user = "*";
static String password = "*";

public static Connection getConnect() {
        Connection con = null;
        try {
            Class.forName("org.postgresql.Driver");
            con = DriverManager.getConnection(url, user, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return con;
    }

需要执行的SQL 添加到一个集合,我用的是

INSERT INTO TableA (select * from TableB)

定义线程池

public static void main(String[] args) {

        List<String> recordList = DbUtils.getSqlList();

        int threadCount = 20;

        if (recordList.size() > 0) {

            System.out.println(new Date() + "准备执行的 SQL Total --- > " + recordList.size());

            final BlockingQueue<String> queue = new ArrayBlockingQueue<String>(recordList.size());

            for (String record : recordList) {
                queue.offer(record);
            }

            ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
            final CountDownLatch countDownLatch = new CountDownLatch(recordList.size());

            for (int i = 0; i < threadCount; i++) {
                executorService.submit(new Runnable() {
                    @Override
                    public void run() {
                        while (true) {
                            if (queue.isEmpty()) {
                                break;
                            }
                            String string = null;
                            try {
                                string = queue.take();

                            } catch (InterruptedException e1) {
                                System.out.println(new Date() + " " + e1);
                            }
                            try {
                            	//单线程执行SQL
                                singleInsert(string);
                                System.out.println(new Date()+"  "+string);
                            } catch (Exception e) {
                                System.out.println(e.getMessage());
                            } finally {
                                countDownLatch.countDown();
                            }
                        }
                    }
                });
            }
            while (countDownLatch.getCount() != 0) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            executorService.shutdownNow();
            System.out.println(new Date() + "多线程导入结束");
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值