测试主线程,MapFetch.javapackage com.sohu.servlet;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.Iterator;

import java.util.List;

import java.util.Map;

import java.util.Map.Entry;

import java.util.Set;

/**

* @author liweihan (liweihan@sohu-inc.com)

* @version 1.0 (2015年7月27日 下午2:18:47)

*/

public class MapFetch {

static Map map = new HashMap();

public static void main(String[] args) {

System.out.println("....test....");

map.put("1", "one");

map.put("2", "two");

map.put("3", "three");

for (int i = 0; i

map.put("key" + i, "value"+i);

}

//1.循环map

/*Set set = map.keySet();

Iterator it = set.iterator();

while(it.hasNext()) {

String key = (String) it.next();

String value = map.get(key);

System.out.println(key + " → " + value);

}*/

//2.循环map

/*Set> se = map.entrySet();

for (Entry en : se) {

System.out.println(en.getKey() + " : " + en.getValue());

}*/

//3.循环map

/*Set> set = map.entrySet();

Iterator> iterator = set.iterator();

while (iterator.hasNext()) {

Entry entry = iterator.next();

String key = entry.getKey();

String value = entry.getValue();

System.out.println( key + " -- " + value);

}*/

//4.合并map

/*Map map1 = new HashMap();

map1.put("1", "one");

Map map2 = new HashMap();

map2.put("2", "two");

map1.putAll(map2);

System.out.println(map1);*/

//5.分割map+多线程

/*int totalSize = map.size();

System.out.println("Map totalSize : " + totalSize);

//线程的数量

int threadNum = 16;

//每个线程处理的数量

int threadSize = totalSize / threadNum;

System.out.println("每个线程处理的数量:" + threadSize);

//join()线程

List threadList = new ArrayList();

for (int i = 0; i

int end;

if (i == threadNum - 1) {

//最后一个线程

end = threadSize + totalSize % threadNum ;

} else {

end = threadSize;

}

int beginNum = i * threadSize;

int endNum = i * threadSize + end;

System.out.println(i + " begin : " + beginNum  + "," + endNum);

int sync = 0;

//分割map

Map mapThread = new HashMap();

for(Entry entry : map.entrySet()) {

sync++;

if (sync > beginNum && sync <= endNum) {

mapThread.put(entry.getKey(), entry.getValue());

}

}

StarRelationThread st = new StarRelationThread(mapThread,map,i);

Thread thread = new Thread(st);

threadList.add(thread);

thread.start();

}

//join()线程-必须等join的线程执行完,才能继续执行下面的代码

for (Thread thread : threadList) {

try {

thread.join();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

map.clear();

System.out.println("------------over---------------");

*/

//6.map的分割

Iterator> iterator = map.entrySet().iterator();

Map mapThread0 = new HashMap();

Map mapThread1 = new HashMap();

Map mapThread2 = new HashMap();

Map mapThread3 = new HashMap();

Map mapThread4 = new HashMap();

Map mapThread5 = new HashMap();

Map mapThread6 = new HashMap();

Map mapThread7 = new HashMap();

Map mapThread8 = new HashMap();

Map mapThread9 = new HashMap();

Map mapThread10 = new HashMap();

Map mapThread_default = new HashMap();

while (iterator.hasNext()) {

Map.Entry entry = iterator.next();

String key = entry.getKey();

String.valueOf(key);

int hashCode = Math.abs(String.valueOf(key).hashCode());

switch (hashCode % 11) {//分割成11份

case 0 :

mapThread0.put(key, map.get(key));

break;

case 1 :

mapThread1.put(key, map.get(key));

break;

case 2 :

mapThread2.put(key, map.get(key));

break;

case 3 :

mapThread3.put(key, map.get(key));

break;

case 4 :

mapThread4.put(key, map.get(key));

break;

case 5 :

mapThread5.put(key, map.get(key));

break;

case 6 :

mapThread6.put(key, map.get(key));

break;

case 7 :

mapThread7.put(key, map.get(key));

break;

case 8 :

mapThread8.put(key, map.get(key));

break;

case 9 :

mapThread9.put(key, map.get(key));

break;

case 10 :

mapThread10.put(key, map.get(key));

break;

default:

mapThread_default.put(key, map.get(key));

break;

}

}

System.out.println(mapThread0.size());

System.out.println(mapThread1.size());

System.out.println(mapThread2.size());

System.out.println(mapThread3.size());

System.out.println(mapThread4.size());

System.out.println(mapThread5.size());

System.out.println(mapThread6.size());

System.out.println(mapThread7.size());

System.out.println(mapThread8.size());

System.out.println(mapThread9.size());

System.out.println(mapThread10.size());

System.out.println(mapThread_default.size());

int a = mapThread0.size() +

mapThread1.size() +

mapThread2.size() +

mapThread3.size() +

mapThread4.size() +

mapThread5.size() +

mapThread6.size() +

mapThread7.size() +

mapThread8.size() +

mapThread9.size() +

mapThread10.size() +

mapThread_default.size();

System.out.println("a:" + a);

}

}

业务处理子线程StarRelationThread.javapackage com.sohu.servlet;

import java.util.Map;

import java.util.Map.Entry;

/**

* @author liweihan (liweihan@sohu-inc.com)

* @version 1.0 (2015年7月27日 下午3:47:09)

*/

public class StarRelationThread implements Runnable{

private Map mapThread ;

private Map map ;

private int threadNum;

public StarRelationThread(Map mapThread ,Map map,int threadNum) {

this.map = map;

this.threadNum = threadNum;

this.mapThread = mapThread;

}

@Override

public void run() {

System.out.println(" 第  " + threadNum + " 个线程处理-开始 ,此线程处理的数量 " + mapThread.size() + ",总的数量为:"+map.size());

System.out.println("处理数据 ,并写入redis中");

if (threadNum > 3) {

try {

Thread.sleep(20000);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

int sync = 0;

for (Entry en : mapThread.entrySet()) {

sync++;

if (sync

System.out.println("key :" + en.getKey() + ", value :" + en.getValue());

}

}

System.out.println(" 第 " + threadNum + " 个线程执行完毕!");

}

}

java 线程map_map集合分割以及多线程处理数据相关推荐

  1. java去重复的集合_如何去除Java中List集合中的重复数据

    1.循环list中的所有元素然后删除重复 public class duplicatRemoval { public static List removeDuplicate(List list){ f ...

  2. Java线程安全集合总结

    转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/120749064 本文出自[赵彦军的博客] Java线程安全StampedLock ...

  3. Java线程安全集合

    并发编程的本质:充分利用CPU资源. 目录 1.准备环境 2.回顾线程和进程 并发和并行 3.回顾多线程 4.Lock锁(重点) Lock锁是一个接口 lock锁与synchronized的区别 传统 ...

  4. java 线程 listview_android使用Thread实现json数据的传递,并且使用ListView显示

    分类: Android平台 2014-03-06 17:06:56 在android 4.0以后,不能在主线程中使用网路资源.所以对于使用json传递的数据,我们要用它直接生成Listview会报一个 ...

  5. Java线程安全StampedLock

    转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/120854573 本文出自[赵彦军的博客] Java线程安全StampedLock ...

  6. Java线程安全Lock、ReentrantLock、ReentrantReadWriteLock

    转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/120750932 本文出自[赵彦军的博客] Java线程安全StampedLock ...

  7. Java Se相关测试题(偏线程、集合)含答案及详解

    Java Se相关测试题(偏线程.集合)(简答.编程)含答案及详解 一.选择.简答题 二.编程题 (编程答案有很多思路,实现方式不同,如果有不同见解可打在评论区或私信) 一.选择.简答题 1.publ ...

  8. java线程安全的set_Java并发编程之set集合的线程安全类你知道吗

    Java并发编程之-set集合的线程安全类 Java中set集合怎么保证线程安全,这种方式你知道吗? 在Java中set集合是 本篇是<凯哥(凯哥并发编程学习>系列之<并发集合系列& ...

  9. 【Java 集合】Java 集合的线程安全性 ( 加锁同步 | java.utils 集合 | 集合属性 | java.util.concurrent 集合 | CopyOnWrite 机制 )

    文章目录 I . Java 集合的线程安全概念 ( 加锁同步 ) II . 线程不安全集合 ( 没有并发需求 推荐使用 ) III . 集合属性说明 IV . 早期的线程安全集合 ( 不推荐使用 ) ...

最新文章

  1. 设计模式(2)工厂方法模式(Factory Method)
  2. springboot配置国际化资源文件 使用themself模板进行解析
  3. Android照片墙完整版,完美结合 内存方案 LruCache 和 硬盘方案 DiskLruCache
  4. 五个常用的Linux监控脚本代码
  5. WinXP启动时自动打开上次关机时未关闭的文件夹
  6. MyBatisPlus_查询分页篇_入门试炼_02
  7. java min 函数的使用方法_【Python】Java程序员学习Python(五)— 函数的定义和使用...
  8. greendao删除其中一条_广东东莞将迎来一条新地铁,全长58公里,设24站,沿途市民有福了...
  9. linux c curl 乱码,curl获取结果乱码的解决方法之CURLOPT_ENCODING(curl/Post请求)
  10. 跨境电商为什么要用ERP系统?
  11. sap后台配置原因代码_SAP FI后台配置清单
  12. UE4添加人物动画之状态机
  13. 电脑怎样查看密码?100%简单实用的方法
  14. 三星s8html查看器,【三星GALAXYS8评测】屏幕:全视屏显示素质经得起数据检验_三星 GALAXY S8_手机评测-中关村在线...
  15. Android集成bilibili播放器以及弹幕
  16. WEB打印控件Lodop的使用
  17. 亮化工程改善城市夜景有什么重要意义
  18. android应用自启分析与S4启动列表
  19. ICLR 2022:​PiCO,基于对比消歧的偏标签学习 丨AI Drive
  20. https 请求需要证书,忽略安全证书

热门文章

  1. oracle中LAG()和LEAD()等分析统计函数的使用方法(统计月增长率)
  2. 七、制作主题(二) Anatomy of a theme
  3. httpclient proxy 方式ssl 死锁 socketRead0问题解决
  4. PHP Lumen Laravel 解决validate方法自定义message无效的问题
  5. 转ORA-28002: the password will expire within 7 days 解决方法
  6. 【白皮书分享】2022新职业教育洞察白皮书:“职”成机遇,“育”见未来.pdf...
  7. 【报告分享】罗兰贝格2019年关于人工智能的十个议题.pdf(附下载链接)
  8. 【NLP保姆级教程】手把手带你RNN文本分类(附代码)
  9. 机器学习算法总结之Boosting:AdaBoost
  10. 如何使用Facebook广告为shopify商店引流