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();
}
}

for miaomiao的更多相关文章

  1. Miaomiao's Geometry

    HDU 4932  Bestcoder Problem Description There are N point on X-axis . Miaomiao would like to cover t ...

  2. HDU 4932 Miaomiao&#39;s Geometry(推理)

    HDU 4932 Miaomiao's Geometry pid=4932" target="_blank" style="">题目链接 题意: ...

  3. hdu 4932 Miaomiao&#39;s Geometry(暴力)

    题目链接:hdu 4932 Miaomiao's Geometry 题目大意:在x坐标上又若干个点,如今要用若干条相等长度的线段覆盖这些点,若一个点被一条线段覆盖,则必须在这条线的左端点或者是右端点, ...

  4. hdu4932 Miaomiao&#39;s Geometry (BestCoder Round #4 枚举)

    题目链接:pid=4932" style="color:rgb(202,0,0); text-decoration:none">http://acm.hdu.edu ...

  5. BestCoder Round #4 Miaomiao&#39;s Geometry (暴力)

    Problem Description There are N point on X-axis . Miaomiao would like to cover them ALL by using seg ...

  6. BestCoder Round #4 之 Miaomiao's Geometry(2014/8/10)

    最后收到邮件说注意小数的问题!此代码并没有过所有数据,请读者参考算法, 自己再去修改一下吧!注意小数问题! Miaomiao's Geometry Time Limit: 2000/1000 MS ( ...

  7. 枚举+贪心 HDOJ 4932 Miaomiao's Geometry

    题目传送门 /* 题意:有n个点,用相同的线段去覆盖,当点在线段的端点才行,还有线段之间不相交 枚举+贪心:有坑点是两个点在同时一条线段的两个端点上,枚举两点之间的距离或者距离一半,尽量往左边放,否则 ...

  8. hdu 4932 Miaomiao&#39;s Geometry(暴力枚举)

    pid=4932">Miaomiao's Geometry                                                               ...

  9. hdu4933 Miaomiao's Function

    水水的计数题,关键在细节. import java.math.BigInteger; import java.util.Scanner; import java.io.*; public class ...

  10. hdu4932 Miaomiao's Geometry

    这是一道搜索题,我们很容易得到目标值的上下界,然后就只能枚举了. 就是将x轴上的点排序之后从左到右依次考察每个点,每个点要么在线段的左端点,要么在线段的右端点. 点编号从0到n-1,从编号为1的点开始 ...

随机推荐

  1. python+request 常用基础学习笔记

    1.pycharm,避免控制台输出的json内容中文出现乱码. #注:乱码为Unicode格式:\u6d4b\u8bd5.加入如下代码后正确返回中文:测试 get_result = r.json() ...

  2. 7月新的开始 - Axure学习05 - 元件库的创建

    元件库的创建 元件库的创建.载入.编辑和删除等操作 元件库的后缀名名:.rplib 学习结果: 实现iPhone6的原型图 分辨率是 1080*1920,现在我们按比例缩小去实现(360*640)

  3. vue 对象更改检测注意事项

  4. js字符串解析与转换成数字

    解析允许字符串中含有非法数字字符,解析按从左至右的顺序,如果遇到非数字字符就停止.而转换不允许出现非数字字符,否则会失败并返回NaN

  5. go常量的定义和枚举类型

    const a,b int = 1,2 const a,b     = 1,2 const ( a = "hello" b,c =3,4 ) 常量数值可作为各种类型使用 枚举类型的 ...

  6. POJ-2115-C Looooops(线性同余方程)

    链接: https://vjudge.net/problem/POJ-2115 题意: A Compiler Mystery: We are given a C-language style for ...

  7. HTML 007 链接

    HTML 链接 HTML 使用超级链接与网络上的另一个文档相连.几乎可以在所有的网页中找到链接.点击链接可以从一张页面跳转到另一张页面. 尝试一下 - 实例 HTML 链接如何在HTML文档中创建链接 ...

  8. 第一章使用JSP/Server技术开发新闻发布系统第一章动态网页开发基础

      一:为什么需要动态网页    由于静态网页的内容是固定的,不能提供个性化和定制化得服务,使用动态网页可真正地与用户实现互动. 二:什么是动态网页  ①:动态网页是指在服务器端运行的,使用程序语言设 ...

  9. 洛谷 P1076 寻宝 题解

    今天又TM考试了...... 这是T1,然后我模拟20分滚粗. Analysis 在每层的时候用编号%这层可以上楼的房间个数就行了. #include<iostream> #include ...

  10. Django系列(一):前期准备

    1.web应用 Web应用程序是一种可以通过web访问的应用程序,程序的最大好处是用户很容易访问应用程序,用户只需要有浏览器即可,不需要再安装其他软件.应用程序有两种模式C/S.B/S.C/S是客户端 ...