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. uni-app之导航配置pages.json

    1.基础配置,各个页面都要在这里边引入. 2.基础配置,头部导航左右上脚的buttons设置. 3.如果没有权限展示底部导航的需求,可以直接在此文件配置底部导航.

  2. for,foreach,$.each()跳出循环的比较

    说起跳出循环,第一时间想起的是 break \ continue,这是经典的for循环. 1.for 循环 先上例子,思考输出结果,体会 break 与 continue 的不同. 1 var arr ...

  3. C#当前运行所在目录集合

    //获取当前进程的完整路径,包含文件名(进程名).string str = this.GetType().Assembly.Location;result: X:\xxx\xxx\xxx.exe (. ...

  4. c# 关于mongo bson转json的问题

    问题解决自:https://stackoverflow.com/questions/27132968/convert-mongodb-bsondocument-to-valid-json-in-c-s ...

  5. KindEditor3.x-自动上传Word图片功能.

    Chrome+IE默认支持粘贴剪切板中的图片,但是我要发布的文章存在word里面,图片多达数十张,我总不能一张一张复制吧?Chrome高版本提供了可以将单张图片转换在BASE64字符串的功能.但是无法 ...

  6. Java上传大文件夹

    javaweb上传文件 上传文件的jsp中的部分 上传文件同样可以使用form表单向后端发请求,也可以使用 ajax向后端发请求 1.通过form表单向后端发送请求 <form id=" ...

  7. grafana 的配置文件,和使用mysql数据库做持久化

    grafana 配置文件 vim /etc/grafana/grafana.ini 下面有些参数在新版中6.x版本中已经过时,请参考下面官网最新帮助文档为准 https://grafana.com/d ...

  8. PowerShell中汉字与ASCII码相互转换

    function asc($param) { $rtn = '' $list = $param -split '' foreach ($char in $list) { if($char -ne '' ...

  9. 【00NOIP提高组】单词接龙

    #include<bits/stdc++.h> using namespace std; ; int n,length; int vis[N]; string str[N]; inline ...

  10. ubuntu16.04安装matlab_R2018a/R2017a

    ubuntu16.04安装matlab_R2018a/R2017a 1. 文件准备 我把Matlab2018a安装镜像及破解文件放在了/home/haes/Downloads/matlab下 2.挂载 ...