单线程和多线程处理1W条数据对比代码
- package study.interview;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Map;
- import java.util.Map.Entry;
- import java.util.concurrent.Callable;
- import java.util.concurrent.ExecutionException;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.concurrent.Future;
- import java.util.Random;
- import java.util.Set;
- public class TestEmailFromList {
- final static String[] list = {"qq.com","126.com","168.com","sina.com","baidu.com","soho.com","yeah.net","139.com","hotmail.com"};
- public static void main(String[] args) throws InterruptedException {
- //email存放容器
- LinkedList<Email> emailList = new LinkedList<Email>();
- //统计各类邮箱使用人数容器
- Map<String, Integer> mapEmail = new HashMap<String, Integer>();
- //统计各类邮箱使用人数容器
- Map<String, Integer> mapEmailPool = new HashMap<String, Integer>();
- Random random=new Random();
- //email原始邮箱初始化
- for (int i = 0; i < 9999; i++) {
- int num = random.nextInt(9);
- emailList.add(new Email(String.valueOf(i+"@"+list[num])));
- }
- // 单线程统计各类邮箱使用人数
- long startTime = System.currentTimeMillis();
- TestEmailFromList.countingBySingleThread(emailList, mapEmail);
- long endTime = System.currentTimeMillis();
- System.out.println("单线程统计用时:"+(endTime-startTime));
- //多线程统计各类邮箱使用人数
- long startTime2 = System.currentTimeMillis();
- //用多少个线程
- TestEmailFromList.countingByMultiThread(emailList, mapEmailPool);
- long endTime2 = System.currentTimeMillis();
- System.out.println("多线程统计用时:"+(endTime2-startTime2));
- }
- /*
- * 单线程统计邮箱使用人数
- */
- public static void countingBySingleThread(LinkedList<Email> emailList,Map<String, Integer> mapEmail){
- for (int i = 0; i <emailList.size(); i++) {
- String key = emailList.get(i).getUserName().split("@")[1];
- if(mapEmail.containsKey(key)){
- int value = mapEmail.get(key);
- mapEmail.put(key, ++value);
- }else{
- mapEmail.put(key, 1);
- }
- }
- printMap(mapEmail);
- }
- /*
- * 多线程统计邮箱使用人数
- */
- public static void countingByMultiThread(LinkedList<Email> emailList,Map<String, Integer> mapEmailPool){
- ExecutorService executorService = Executors.newCachedThreadPool();
- List<Future<Map<String, Integer>>> resultList = new ArrayList<Future<Map<String, Integer>>>();
- for (int i = 0; i < 4; i++) {
- LinkedList<Email> eList = null;
- if(i==3){
- eList = new LinkedList<Email>(emailList.subList(i*2500,(i+1)*2500-1));
- }else{
- eList = new LinkedList<Email>(emailList.subList(i*2500,(i+1)*2500));
- }
- System.out.println(eList.getFirst().getUserName()+"----"+eList.getLast().getUserName());
- //使用ExecutorService执行Callable类型的任务,并将结果保存在future变量中
- Future<Map<String, Integer>> future = executorService.submit(new TaskWithResultMap( eList,mapEmailPool));
- //将任务执行结果存储到List中
- resultList.add(future);
- }
- //遍历任务的结果
- for (Future<Map<String, Integer>> fs : resultList) {
- try {
- System.out.println(fs.get());
- } catch (InterruptedException e) {
- e.printStackTrace();
- } catch (ExecutionException e) {
- e.printStackTrace();
- }finally{
- executorService.shutdown();
- }
- }
- printMap(mapEmailPool);
- }
- /*
- * 输出map
- */
- public static void printMap(Map<String, Integer> mapEmail){
- Set<Entry<String, Integer>> set = mapEmail.entrySet();
- for (Entry<String, Integer> entry : set) {
- System.out.println("使用"+entry.getKey()+"的人共"+entry.getValue());
- }
- }
- }
- class TaskWithResultMap implements Callable<Map<String, Integer>>{
- LinkedList<Email> emailList;
- Map<String, Integer> mapEmailPool;
- public TaskWithResultMap(LinkedList<Email> emailList,Map<String, Integer> mapEmailPool){
- this.emailList = emailList;
- this.mapEmailPool = mapEmailPool;
- }
- @Override
- public Map<String, Integer> call() throws Exception {
- synchronized (mapEmailPool) {
- for (int i = 0; i <emailList.size(); i++) {
- String key = emailList.get(i).getUserName().split("@")[1];
- if(mapEmailPool.containsKey(key)){
- int value = mapEmailPool.get(key);
- mapEmailPool.put(key, ++value);
- }else{
- mapEmailPool.put(key, 1);
- }
- }
- }
- return mapEmailPool;
- }
- }
- class MyThread implements Runnable {
- LinkedList<Email> emailList;
- Map<String, Integer> mapEmailPool;
- public MyThread(LinkedList<Email> emailList,Map<String, Integer> mapEmailPool) {
- this.emailList = emailList;
- this.mapEmailPool = mapEmailPool;
- }
- public void run() {
- while(true){
- synchronized (mapEmailPool) {
- try {
- for (int i = 0; i <emailList.size(); i++) {
- String key = emailList.get(i).getUserName().split("@")[1];
- if(mapEmailPool.containsKey(key)){
- int value = mapEmailPool.get(key);
- mapEmailPool.put(key, ++value);
- }else{
- mapEmailPool.put(key, 1);
- }
- }
- } catch (Exception e) {
- System.out.println(Thread.currentThread().getName()+"异常");
- }
- }
- }
- }
- }
- class Email {
- String username;
- public Email() {
- }
- public Email(String username) {
- this.username = username;
- }
- public String getUserName() {
- return username;
- }
- }
单线程和多线程处理1W条数据对比代码的更多相关文章
- 主要看思路:区域数据去重 + JavaScript一次性展示几万条数据实例代码
近期做1功能,Gis地图 基于百度地图api , 会遇到的问题的, 如后台接口给的数据很多,大几千上万的,如果拿了数据直接渲染dom ,这滋味爽爽的. 再遇上 客户端浏览器悲催的,这卡顿就来了... ...
- 面试官:"准备用HashMap存1w条数据,构造时传10000还会触发扩容吗?"
// 预计存入 1w 条数据,初始化赋值 10000,避免 resize. HashMap<String,String> map = new HashMap<>(10000) ...
- 面试官:”准备用HashMap存1w条数据,构造时传10000会触发扩容吗?“
通常在初始化 HashMap 时,初始容量都是根据业务来的,而不会是一个固定值,为此我们需要有一个特殊处理的方式,就是将预期的初始容量,再除以 HashMap 的装载因子,默认时就是除以 0.75. ...
- EDG夺冠!用Python分析22.3万条数据:粉丝都疯了!
一.EDG夺冠信息 11月6日,在英雄联盟总决赛中,EDG战队以3:2战胜韩国队,获得2021年英雄联盟全球总决赛冠军,这个比赛在全网各大平台也是备受瞩目: 1.微博热搜第一名,截止2021-11-1 ...
- 从1KW条数据中筛选出1W条最大的数
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...
- json代码驾照考题批量加入MySQL数据库 ps.executeUpdate()永远只能悲催的加一条数据 去掉id主键自增 用foreach循环数据库只能出现一条语句
package com.swift; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStrea ...
- Java备份约9亿条数据
需求:有一张表9亿多条数据,数据加索引总数据量61GB.考虑到这张表的大部分数据都不会再被使用并且大数据量可能影响整库的性能,所以决定将表里某一个时刻之前的数据备份到一张新表中,待备份完成后将旧表中已 ...
- 极限挑战—C#+ODP 100万条数据导入Oracle数据库仅用不到1秒
链接地址:http://www.cnblogs.com/armyfai/p/4646213.html 要:在这里我们将看到的是C#中利用ODP实现在Oracle数据库中瞬间导入百万级数据,这对快速批量 ...
- 极限挑战—C#100万条数据导入SQL SERVER数据库仅用4秒 (附源码)
原文:极限挑战-C#100万条数据导入SQL SERVER数据库仅用4秒 (附源码) 实际工作中有时候需要把大量数据导入数据库,然后用于各种程序计算,本实验将使用5中方法完成这个过程,并详细记录各种方 ...
随机推荐
- LCD1602小程序
1显示数据 typedef struct { unsigned long int mL_data; unsigned long int L_data; unsigned long int M3_dat ...
- Springboot与日志
日志框架 比如开发一个大型系统:1.System.out.println(""):将关键数据打印在控制台:去掉?写在一个文件?2.框架来记录系统的一些运行时信息:日志框架 :riz ...
- 51Nod 1509 加长棒(隔板法)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1509 思路: 直接去解可行的方法有点麻烦,所以应该用总的方法去减去不可行 ...
- mybatis报Invalid bound statement (not found) 分析
解决问题的步骤,请参考: 1.mapper.xml要和对应的mapper接口在同一个包下,包名要一模一样. 2.Mapper接口中的方法在Mapper.xml中没有,然后执行Mapper接口的方法 ...
- 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 ...
- Nginx 多进程连接请求/事件分发流程分析
Nginx使用多进程的方法进行任务处理,每个worker进程只有一个线程,单线程循环处理全部监听的事件.本文重点分析一下多进程间的负载均衡问题以及Nginx多进程事件处理流程,方便大家自己写程序的时候 ...
- Kotlin中的object 与companion object的区别
之前写了一篇Kotlin中常量和静态方法的文章,最近有人提出一个问题,在companion object中调用外部的成员变量会调用不到,这才意识到问题,本篇文章会带着这个疑问来解决问题. 一. obj ...
- 经典线程同步问题(生产者&消费者)--Java实现
生产者-消费者(producer-consumer)问题是一个著名的线程同步问题.它描述的是:有一群生产者线程在生产产品,并将这些产品提供给消费者线程去消费. 为使生产者与消费者之间能够并发执行,在两 ...
- Android------实现图片双击放大,缩小,左右滑动的多种方式
项目中常常有图片浏览功能.像微信朋友圈图片浏览,QQ空间照片浏览 的功能. 实现图片双击放大,缩小,左右滑动等效果. 来看看我的效果图,希望能满足你的要求 前三个button按钮是参考网上的多种实 ...
- android--------Universal-Image-Loader图片加载框架和结合LruCache缓存图片
本博客包含包含Android-Universal-Image-Loader 网络图片加载框架实现图片加载和结合universal-image-loader与LruCache来自定义缓存图片,可以设置缓 ...