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的点开始 ...
随机推荐
- go语言开发IDE
软件下载及绿化方法 GoLand-2018.3 链接:https://pan.baidu.com/s/15AKPDIJIN86vxfriHBjE-g 提取码:060h 选择路径的时候,去掉路径名的版本 ...
- aiohttp 支持异步的网络请求模块
通常在进行网络数据采集时候我们会用到requests,urllib等模块,但是这些模块在使用中并不支持异步,所以今天我们介绍一个支持异步网络请求的模块aiohttp. 首先我们使用flask简单的搭一 ...
- LightOJ-1020-A Childhood Game(博弈)
链接: https://vjudge.net/problem/LightOJ-1020 题意: Alice and Bob are playing a game with marbles; you m ...
- Spring MVC的方法返回值和参数传递
1. SpringMVC方法的返回值类型 3.1String类作为返回值 3.1.1Controller层 /** * 返回值类型为String时,一般用于返回视图名称 * 1.当方法返回值为null ...
- navigator对象及属性(userAgent)(扩展)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- trigger 和 triggerHandler(),自定义事件
trigger 和 triggerHandler(),自定义事件 语法: $(selector).trigger(event,[param1,param2,...]) 1,event 必需.规定指定元 ...
- exam9.6&&7
emmm 改题稍紧张,以后几篇并一起写 9.6 (前十并没有参加本次考试) 于是我就rank8了 一道题一道题来 先说T1: 显然是一个高精度GCD,于是打算用计算器算一下时间复杂度 众所周知gcd是 ...
- 爬虫(二):Urllib库详解
什么是Urllib: python内置的HTTP请求库 urllib.request : 请求模块 urllib.error : 异常处理模块 urllib.parse: url解析模块 urllib ...
- Python3循环
Python中while语句的一般形式: while 判断条件: 语句 同样需要注意冒号和缩进,另外在Python中没有do…while循环 下面的实例计算1到100总和 ##calc.py n = ...
- js的老生代垃圾回收
推荐阅读:<JS 闯关记>之垃圾回收和内存管理 常见的垃圾回收有2种策略:标记清除 和 引用计数 标记清除 会遍历堆中所有的对象,然后标记活的对象,在标记完成后,销毁所有没有被标记的对象. ...