zoukankan      html  css  js  c++  java
  • for miaomiao

    package com.mytest.formiaomiao;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.concurrent.*;
    
    public abstract class MultiThreadService<T, R> {
    
        public List<T> process(List<R> resources, int threadNumber) {
    
            ExecutorService executorService = Executors.newFixedThreadPool(threadNumber);
            List<Future<T>> futures = new ArrayList<>();
    
            for (R resource : resources) {
                SubTask subTask = new SubTask(resource);
                futures.add(executorService.submit(subTask));
            }
    
            List<T> resultList = new ArrayList<>();
            try {
                for (Future<T> future : futures) {
                    if (future.get() != null) {
                        resultList.add(future.get());
                    }
                }
            } catch (InterruptedException | ExecutionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                executorService.shutdown();
            }
    
            return resultList;
    
        }
    
        protected abstract T doSubTask(R resource);
    
        private class SubTask implements Callable<T> {
    
            private R resource;
    
            public SubTask(R resource) {
                this.resource = resource;
            }
    
            @Override
            public T call() throws Exception {
                return doSubTask(resource);
            }
        }
    }
    package com.mytest.formiaomiao;
    
    import java.sql.Connection;
    import java.sql.Statement;
    import java.util.Map;
    
    public class MyMultiThreadService extends MultiThreadService<String, Map<Integer, String>> {
    
        @Override
        protected String doSubTask(Map<Integer, String> resource) {
    
            String result = "OK";
    
            try(Connection conn = MyUtils.getConnection(); Statement stat = conn.createStatement()) {
                StringBuilder sb = new StringBuilder("insert formiaomiao.student (student_id, student_name) values ");
                resource.forEach((k, v) -> {
                    sb.append(String.format("(%d, '%s')", k, v)).append(",");
                });
                String sql = sb.substring(0, sb.length() - 1);
                stat.execute(sql);
            } catch (Exception e) {
                result = "NG";
            }
    
            return result;
        }
    }
    package com.mytest.formiaomiao;
    
    import org.apache.commons.lang3.RandomStringUtils;
    
    import java.time.LocalDateTime;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    import java.util.TreeMap;
    import java.util.concurrent.Future;
    
    public class MyTest {
    
        public static void main(String[] args) {
    
            System.out.println("[1] " + LocalDateTime.now());
            List<Map<Integer, String>> studentMapList = createListMap();
            System.out.println(studentMapList.size());
            System.out.println("[2] " + LocalDateTime.now());
    
            MyMultiThreadService myThreadService = new MyMultiThreadService();
            List<String> results = myThreadService.process(studentMapList, 5);
            System.out.println("[3] " + LocalDateTime.now());
        }
    
        private static List<Map<Integer, String>> createListMap() {
            List<Map<Integer, String>> studentMapList = new ArrayList<>();
    
            Map<Integer, String> paramMap = new TreeMap<>();
            for (int count = 1; count <= 1000 * 1000; count++) {
                paramMap.put(count, RandomStringUtils.randomAlphabetic(6));
                if (count % 100000 == 0) {
                    studentMapList.add(paramMap);
                    paramMap = new TreeMap<>();
                }
    
            }
            return studentMapList;
        }
    }
    package com.mytest.formiaomiao;
    
    import com.zaxxer.hikari.HikariDataSource;
    
    import java.sql.Connection;
    import java.sql.SQLException;
    
    public class MyUtils {
    
        public static Connection getConnection() throws SQLException {
            HikariDataSource ds = new HikariDataSource();
            ds.setJdbcUrl("jdbc:mysql://localhost:3306/formiaomiao?characterEncoding=utf-8&serverTimezone=UTC");
            ds.setUsername("root");
            ds.setPassword("20546737");
            return ds.getConnection();
        }
    }
  • 相关阅读:
    Golang服务器热重启、热升级、热更新(safe and graceful hot-restart/reload http server)详解
    如果清空Isilon cluster上的文件,Shadow Store和data reduction的统计信息也会一并清空么?
    合并从B站下载的分开的音频和视频
    使用Notepad++远程编辑WinSCP中打开的文本文件报错“file xxx does exist anymore”
    Leetcode 1143. 最长公共子序列(LCS)动态规划
    Leetcode 126 127 单词接龙i&ii
    如何在一个Docker中同时运行多个程序进程?
    dockerfile cmd使用
    Leetcode 160.相交链表
    Leetcode 912. 排序数组
  • 原文地址:https://www.cnblogs.com/storml/p/11721125.html
Copyright © 2011-2022 走看看