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();
}
}
for miaomiao的更多相关文章
- Miaomiao's Geometry
HDU 4932 Bestcoder Problem Description There are N point on X-axis . Miaomiao would like to cover t ...
- HDU 4932 Miaomiao's Geometry(推理)
HDU 4932 Miaomiao's Geometry pid=4932" target="_blank" style="">题目链接 题意: ...
- hdu 4932 Miaomiao's Geometry(暴力)
题目链接:hdu 4932 Miaomiao's Geometry 题目大意:在x坐标上又若干个点,如今要用若干条相等长度的线段覆盖这些点,若一个点被一条线段覆盖,则必须在这条线的左端点或者是右端点, ...
- hdu4932 Miaomiao's Geometry (BestCoder Round #4 枚举)
题目链接:pid=4932" style="color:rgb(202,0,0); text-decoration:none">http://acm.hdu.edu ...
- BestCoder Round #4 Miaomiao's Geometry (暴力)
Problem Description There are N point on X-axis . Miaomiao would like to cover them ALL by using seg ...
- BestCoder Round #4 之 Miaomiao's Geometry(2014/8/10)
最后收到邮件说注意小数的问题!此代码并没有过所有数据,请读者参考算法, 自己再去修改一下吧!注意小数问题! Miaomiao's Geometry Time Limit: 2000/1000 MS ( ...
- 枚举+贪心 HDOJ 4932 Miaomiao's Geometry
题目传送门 /* 题意:有n个点,用相同的线段去覆盖,当点在线段的端点才行,还有线段之间不相交 枚举+贪心:有坑点是两个点在同时一条线段的两个端点上,枚举两点之间的距离或者距离一半,尽量往左边放,否则 ...
- hdu 4932 Miaomiao's Geometry(暴力枚举)
pid=4932">Miaomiao's Geometry ...
- hdu4933 Miaomiao's Function
水水的计数题,关键在细节. import java.math.BigInteger; import java.util.Scanner; import java.io.*; public class ...
- hdu4932 Miaomiao's Geometry
这是一道搜索题,我们很容易得到目标值的上下界,然后就只能枚举了. 就是将x轴上的点排序之后从左到右依次考察每个点,每个点要么在线段的左端点,要么在线段的右端点. 点编号从0到n-1,从编号为1的点开始 ...
随机推荐
- 用vue开发一个所谓的数独
1.前言 最近的后台管理系统页面,功能暂时没有新的需求,就在想首页放什么东西,最近我想到的就是放个所谓的数独,为什么是所谓的数独,因为规则不同于标准的数独,只要求每一行每一列数字不一样就可以了!这个实 ...
- Codeforces Round #533 (Div. 2) D. Kilani and the Game(BFS)
题目链接:https://codeforces.com/contest/1105/problem/D 题意:p 个人在 n * m 的地图上扩展自己的城堡范围,每次最多走 a_i 步(曼哈顿距离),按 ...
- Linux Swap是干嘛的?
swap是干嘛的? 在Linux下,SWAP的作用类似Windows系统下的“虚拟内存”.当物理内存不足时,拿出部分硬盘空间当SWAP分区(虚拟成内存)使用,从而解决内存容量不足的情况. SWAP意思 ...
- Poj 2104 K-th Number(主席树&&整体二分)
K-th Number Time Limit: 20000MS Memory Limit: 65536K Case Time Limit: 2000MS Description You are wor ...
- thinkphp5/php cors跨域处理
现在做项目,很多都是前后端分离.也就是前段,后端都有自己的域名. 那么前段请求后端接口的时候,就会出现跨域问题.出现跨域的问题,主要 是浏览器的安全策略-同源策略.那么怎么解决跨域问题呢,抛出主角 C ...
- 【原创】go语言学习(六)函数详解2
目录 变量作用域和可见性 匿名函数 闭包 课后练习 变量作用域和可见性 1.全局变量量,在程序整个生命周期有效. var a int = 10 2.局部变量量,分为两种: 1)函数内定义, 2)语句句 ...
- xshel链接linuxl安装nginx
原文链接:https://blog.csdn.net/Sweet__dream/article/details/78256952?utm_source=blogxgwz9 这个连接更详细:https: ...
- mvn ssm 异常 org.springframework.beans.factory.BeanCreationException:Error creating bean with name 'multipartResolver'
解决方案: 添加 commons-fileupload-1.2.jar <!-- https://mvnrepository.com/artifact/commons-fileupload/co ...
- 不用中间变量交换a和b的值?
a = b = a = a+b b = a-b a = a-b print(a,b) a = b = a = a^b b = b^a a = a^b print(a,b) a = b = a,b = ...
- FOI冬令营 Day2
目录 T1.直径(diameter) 传送门 Code T2.定价(price) 传送门 Code T3.排序(sort) 传送门 Code T1.直径(diameter) 传送门 Code //20 ...