测试主线程,MapFetch.java

package 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 < 300000; 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 < threadNum; 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.java

package 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 < 2) { System.out.println("key :" + en.getKey() + ", value :" + en.getValue()); } } System.out.println(" 第 " + threadNum + " 个线程执行完毕!"); }}