1. package study.interview;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.LinkedList;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.Map.Entry;
  9. import java.util.concurrent.Callable;
  10. import java.util.concurrent.ExecutionException;
  11. import java.util.concurrent.ExecutorService;
  12. import java.util.concurrent.Executors;
  13. import java.util.concurrent.Future;
  14. import java.util.Random;
  15. import java.util.Set;
  16.  
  17. public class TestEmailFromList {
  18. final static String[] list = {"qq.com","126.com","168.com","sina.com","baidu.com","soho.com","yeah.net","139.com","hotmail.com"};
  19. public static void main(String[] args) throws InterruptedException {
  20. //email存放容器
  21. LinkedList<Email> emailList = new LinkedList<Email>();
  22. //统计各类邮箱使用人数容器
  23. Map<String, Integer> mapEmail = new HashMap<String, Integer>();
  24. //统计各类邮箱使用人数容器
  25. Map<String, Integer> mapEmailPool = new HashMap<String, Integer>();
  26. Random random=new Random();
  27. //email原始邮箱初始化
  28. for (int i = 0; i < 9999; i++) {
  29. int num = random.nextInt(9);
  30. emailList.add(new Email(String.valueOf(i+"@"+list[num])));
  31. }
  32.  
  33. // 单线程统计各类邮箱使用人数
  34. long startTime = System.currentTimeMillis();
  35. TestEmailFromList.countingBySingleThread(emailList, mapEmail);
  36. long endTime = System.currentTimeMillis();
  37. System.out.println("单线程统计用时:"+(endTime-startTime));
  38.  
  39. //多线程统计各类邮箱使用人数
  40. long startTime2 = System.currentTimeMillis();
  41. //用多少个线程
  42. TestEmailFromList.countingByMultiThread(emailList, mapEmailPool);
  43. long endTime2 = System.currentTimeMillis();
  44. System.out.println("多线程统计用时:"+(endTime2-startTime2));
  45.  
  46. }
  47. /*
  48. * 单线程统计邮箱使用人数
  49. */
  50. public static void countingBySingleThread(LinkedList<Email> emailList,Map<String, Integer> mapEmail){
  51. for (int i = 0; i <emailList.size(); i++) {
  52. String key = emailList.get(i).getUserName().split("@")[1];
  53. if(mapEmail.containsKey(key)){
  54. int value = mapEmail.get(key);
  55. mapEmail.put(key, ++value);
  56. }else{
  57. mapEmail.put(key, 1);
  58. }
  59. }
  60. printMap(mapEmail);
  61. }
  62. /*
  63. * 多线程统计邮箱使用人数
  64. */
  65. public static void countingByMultiThread(LinkedList<Email> emailList,Map<String, Integer> mapEmailPool){
  66. ExecutorService executorService = Executors.newCachedThreadPool();
  67. List<Future<Map<String, Integer>>> resultList = new ArrayList<Future<Map<String, Integer>>>();
  68. for (int i = 0; i < 4; i++) {
  69. LinkedList<Email> eList = null;
  70. if(i==3){
  71. eList = new LinkedList<Email>(emailList.subList(i*2500,(i+1)*2500-1));
  72. }else{
  73. eList = new LinkedList<Email>(emailList.subList(i*2500,(i+1)*2500));
  74. }
  75. System.out.println(eList.getFirst().getUserName()+"----"+eList.getLast().getUserName());
  76. //使用ExecutorService执行Callable类型的任务,并将结果保存在future变量中
  77. Future<Map<String, Integer>> future = executorService.submit(new TaskWithResultMap( eList,mapEmailPool));
  78. //将任务执行结果存储到List中
  79. resultList.add(future);
  80.  
  81. }
  82.  
  83. //遍历任务的结果
  84. for (Future<Map<String, Integer>> fs : resultList) {
  85. try {
  86. System.out.println(fs.get());
  87. } catch (InterruptedException e) {
  88. e.printStackTrace();
  89. } catch (ExecutionException e) {
  90. e.printStackTrace();
  91. }finally{
  92. executorService.shutdown();
  93. }
  94. }
  95.  
  96. printMap(mapEmailPool);
  97. }
  98.  
  99. /*
  100. * 输出map
  101. */
  102. public static void printMap(Map<String, Integer> mapEmail){
  103. Set<Entry<String, Integer>> set = mapEmail.entrySet();
  104. for (Entry<String, Integer> entry : set) {
  105. System.out.println("使用"+entry.getKey()+"的人共"+entry.getValue());
  106. }
  107. }
  108.  
  109. }
  110.  
  111. class TaskWithResultMap implements Callable<Map<String, Integer>>{
  112. LinkedList<Email> emailList;
  113. Map<String, Integer> mapEmailPool;
  114. public TaskWithResultMap(LinkedList<Email> emailList,Map<String, Integer> mapEmailPool){
  115. this.emailList = emailList;
  116. this.mapEmailPool = mapEmailPool;
  117. }
  118.  
  119. @Override
  120. public Map<String, Integer> call() throws Exception {
  121. synchronized (mapEmailPool) {
  122. for (int i = 0; i <emailList.size(); i++) {
  123. String key = emailList.get(i).getUserName().split("@")[1];
  124. if(mapEmailPool.containsKey(key)){
  125. int value = mapEmailPool.get(key);
  126. mapEmailPool.put(key, ++value);
  127. }else{
  128. mapEmailPool.put(key, 1);
  129. }
  130. }
  131. }
  132. return mapEmailPool;
  133. }
  134.  
  135. }
  136.  
  137. class MyThread implements Runnable {
  138. LinkedList<Email> emailList;
  139. Map<String, Integer> mapEmailPool;
  140.  
  141. public MyThread(LinkedList<Email> emailList,Map<String, Integer> mapEmailPool) {
  142. this.emailList = emailList;
  143. this.mapEmailPool = mapEmailPool;
  144. }
  145.  
  146. public void run() {
  147. while(true){
  148. synchronized (mapEmailPool) {
  149. try {
  150. for (int i = 0; i <emailList.size(); i++) {
  151. String key = emailList.get(i).getUserName().split("@")[1];
  152. if(mapEmailPool.containsKey(key)){
  153. int value = mapEmailPool.get(key);
  154. mapEmailPool.put(key, ++value);
  155. }else{
  156. mapEmailPool.put(key, 1);
  157. }
  158.  
  159. }
  160. } catch (Exception e) {
  161. System.out.println(Thread.currentThread().getName()+"异常");
  162. }
  163.  
  164. }
  165. }
  166. }
  167.  
  168. }
  169.  
  170. class Email {
  171. String username;
  172.  
  173. public Email() {
  174. }
  175.  
  176. public Email(String username) {
  177. this.username = username;
  178. }
  179.  
  180. public String getUserName() {
  181. return username;
  182. }
  183. }

单线程和多线程处理1W条数据对比代码的更多相关文章

  1. 主要看思路:区域数据去重 + JavaScript一次性展示几万条数据实例代码

    近期做1功能,Gis地图 基于百度地图api , 会遇到的问题的, 如后台接口给的数据很多,大几千上万的,如果拿了数据直接渲染dom ,这滋味爽爽的. 再遇上 客户端浏览器悲催的,这卡顿就来了... ...

  2. 面试官:"准备用HashMap存1w条数据,构造时传10000还会触发扩容吗?"

    // 预计存入 1w 条数据,初始化赋值 10000,避免 resize. HashMap<String,String> map = new HashMap<>(10000) ...

  3. 面试官:”准备用HashMap存1w条数据,构造时传10000会触发扩容吗?“

    通常在初始化 HashMap 时,初始容量都是根据业务来的,而不会是一个固定值,为此我们需要有一个特殊处理的方式,就是将预期的初始容量,再除以 HashMap 的装载因子,默认时就是除以 0.75. ...

  4. EDG夺冠!用Python分析22.3万条数据:粉丝都疯了!

    一.EDG夺冠信息 11月6日,在英雄联盟总决赛中,EDG战队以3:2战胜韩国队,获得2021年英雄联盟全球总决赛冠军,这个比赛在全网各大平台也是备受瞩目: 1.微博热搜第一名,截止2021-11-1 ...

  5. 从1KW条数据中筛选出1W条最大的数

    using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...

  6. json代码驾照考题批量加入MySQL数据库 ps.executeUpdate()永远只能悲催的加一条数据 去掉id主键自增 用foreach循环数据库只能出现一条语句

    package com.swift; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStrea ...

  7. Java备份约9亿条数据

    需求:有一张表9亿多条数据,数据加索引总数据量61GB.考虑到这张表的大部分数据都不会再被使用并且大数据量可能影响整库的性能,所以决定将表里某一个时刻之前的数据备份到一张新表中,待备份完成后将旧表中已 ...

  8. 极限挑战—C#+ODP 100万条数据导入Oracle数据库仅用不到1秒

    链接地址:http://www.cnblogs.com/armyfai/p/4646213.html 要:在这里我们将看到的是C#中利用ODP实现在Oracle数据库中瞬间导入百万级数据,这对快速批量 ...

  9. 极限挑战—C#100万条数据导入SQL SERVER数据库仅用4秒 (附源码)

    原文:极限挑战-C#100万条数据导入SQL SERVER数据库仅用4秒 (附源码) 实际工作中有时候需要把大量数据导入数据库,然后用于各种程序计算,本实验将使用5中方法完成这个过程,并详细记录各种方 ...

随机推荐

  1. LCD1602小程序

    1显示数据 typedef struct { unsigned long int mL_data; unsigned long int L_data; unsigned long int M3_dat ...

  2. Springboot与日志

    日志框架 比如开发一个大型系统:1.System.out.println(""):将关键数据打印在控制台:去掉?写在一个文件?2.框架来记录系统的一些运行时信息:日志框架 :riz ...

  3. 51Nod 1509 加长棒(隔板法)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1509 思路: 直接去解可行的方法有点麻烦,所以应该用总的方法去减去不可行 ...

  4. mybatis报Invalid bound statement (not found) 分析

      解决问题的步骤,请参考: 1.mapper.xml要和对应的mapper接口在同一个包下,包名要一模一样. 2.Mapper接口中的方法在Mapper.xml中没有,然后执行Mapper接口的方法 ...

  5. msgsrvmgr.cpp:5:37: fatal error: kdl_conversions/kdl_msg.h: No such file or directory #include <kdl_conversions/kdl_msg.h>

    /home/xxx/ros_workspace/src/bp_protocol_bridge/protospot/src/msgsrvmgr.cpp::: fatal error: kdl_conve ...

  6. Nginx 多进程连接请求/事件分发流程分析

    Nginx使用多进程的方法进行任务处理,每个worker进程只有一个线程,单线程循环处理全部监听的事件.本文重点分析一下多进程间的负载均衡问题以及Nginx多进程事件处理流程,方便大家自己写程序的时候 ...

  7. Kotlin中的object 与companion object的区别

    之前写了一篇Kotlin中常量和静态方法的文章,最近有人提出一个问题,在companion object中调用外部的成员变量会调用不到,这才意识到问题,本篇文章会带着这个疑问来解决问题. 一. obj ...

  8. 经典线程同步问题(生产者&消费者)--Java实现

    生产者-消费者(producer-consumer)问题是一个著名的线程同步问题.它描述的是:有一群生产者线程在生产产品,并将这些产品提供给消费者线程去消费. 为使生产者与消费者之间能够并发执行,在两 ...

  9. Android------实现图片双击放大,缩小,左右滑动的多种方式

    项目中常常有图片浏览功能.像微信朋友圈图片浏览,QQ空间照片浏览 的功能. 实现图片双击放大,缩小,左右滑动等效果. 来看看我的效果图,希望能满足你的要求   前三个button按钮是参考网上的多种实 ...

  10. android--------Universal-Image-Loader图片加载框架和结合LruCache缓存图片

    本博客包含包含Android-Universal-Image-Loader 网络图片加载框架实现图片加载和结合universal-image-loader与LruCache来自定义缓存图片,可以设置缓 ...