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的点开始 ...
随机推荐
- 【Java】《Java程序设计基础教程》第一章学习
一.Java概述 1.介绍了Java语言的由来 2.Java语言的特点:简单.面向对象.分布式.高效解释执行.健壮.安全.结构中立.可移植.高效率.多线程.动态 3.Java语言的实现机制,引入虚拟机 ...
- TinyMCE 工具栏配置
plugins: { type: [String, Array], default: 'lists image media wordcount advlist bbcode code charmap ...
- / WebAPP开发与小程序 / 步骤一 · 4-5 地图搜索与poi结合(2)
/ WebAPP开发与小程序 / 步骤一 · 4-5 地图搜索与poi结合(2) 在地图中搜索指定对象时,搜索结果可以显示出每个对象的图片,就差这个不会了
- 棋盘n皇后问题-递归
题目:在n*n的棋盘上,放n个皇后,互不攻击(不可在同行/列/对角线) 分析:将棋盘抽象成一个一维数组[0,1,2......,n*n-1],x=~~(i/n)取整,y=i%n; de ...
- leetcode解题报告(16):Move Zeroes
描述 Given an array nums, write a function to move all 0's to the end of it while maintaining the rela ...
- 初识QuartusII 9.0(破解,半加器的仿真,综合:上)
由于在意大利期间,用的xilinx公司的ZYBO板子,相应的软件用ISE,SDK.回国买了altera公司的板子,自然也要学习国内较流行的软件(TB大西瓜家,因此相关例程也是大部分引用他家).Quar ...
- Python Tinker学习笔记
一直在简单看看python,这次项目需要做个界面,最好是要跨平台的,之前考虑QT,但是树莓派上QT跨平台编译一直装这有问题,后来发现Python不就可以么? 于是决定用python做个界面,但是做界面 ...
- geometry_msgs/PoseStamped 类型的变量的构造
#navpoint.msg geometry_msgs/PoseStamped target_pose uint8 floor uint8 type target_pose 的类型为geometry_ ...
- .Net类库 压缩文件 与 Ionic.Zip 批量压缩不同目录文件与解压 文件
using System; using System.IO; using System.IO.Compression; using System.Linq; using System.Text; us ...
- (转) hive调优(2)
hive 调优(二)参数调优汇总 在hive调优(一) 中说了一些常见的调优,但是觉得参数涉及不多,补充如下 1.设置合理solt数 mapred.tasktracker.map.tasks.maxi ...