测试主线程,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<String, String> map = new HashMap<String, String>();

	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<String> set = map.keySet();
		Iterator<String> it = set.iterator();
		while(it.hasNext()) {
			String key = (String) it.next();
			String value = map.get(key);
			System.out.println(key + " → " + value);
		}*/
		
		
		//2.循环map
		/*Set<Entry<String,String>> se = map.entrySet();
		for (Entry<String,String> en : se) {
			System.out.println(en.getKey() + " : " + en.getValue());
		}*/
		
		//3.循环map
/*		Set<Entry<String, String>> set = map.entrySet();
		Iterator<Entry<String, String>> iterator = set.iterator();
		
		while (iterator.hasNext()) {
			Entry<String, String> entry = iterator.next();
			String key = entry.getKey();
			String value = entry.getValue();
			System.out.println( key + " -- " + value);
		}*/
		
		//4.合并map
		/*Map<String, String> map1 = new HashMap<String, String>();
		map1.put("1", "one");
		
		Map<String, String> map2 = new HashMap<String, String>();
		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<Thread> threadList = new ArrayList<Thread>();
		
		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<String, String> mapThread = new HashMap<String, String>();
			for(Entry<String, String> 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<Entry<String, String>> iterator = map.entrySet().iterator();
		Map<String, String> mapThread0 = new HashMap<String, String>();
		Map<String, String> mapThread1 = new HashMap<String, String>();
		Map<String, String> mapThread2 = new HashMap<String, String>();
		Map<String, String> mapThread3 = new HashMap<String, String>();
		Map<String, String> mapThread4 = new HashMap<String, String>();
		Map<String, String> mapThread5 = new HashMap<String, String>();
		Map<String, String> mapThread6 = new HashMap<String, String>();
		Map<String, String> mapThread7 = new HashMap<String, String>();
		Map<String, String> mapThread8 = new HashMap<String, String>();
		Map<String, String> mapThread9 = new HashMap<String, String>();
		Map<String, String> mapThread10 = new HashMap<String, String>();
		Map<String, String> mapThread_default = new HashMap<String, String>();
		
		while (iterator.hasNext()) {
			Map.Entry<String, String> 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<String, String> mapThread ;
	private Map<String, String> map ;
	private int threadNum;
	
	public StarRelationThread(Map<String, String> mapThread ,Map<String, String> 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<String, String> en : mapThread.entrySet()) {
			sync++;
			if (sync < 2) {
				System.out.println("key :" + en.getKey() + ", value :" + en.getValue());
			}
		}
		System.out.println(" 第 " + threadNum + " 个线程执行完毕!");
	}

}