java 线程map_map集合分割以及多线程处理数据

测试主线程,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 + " 个线程执行完毕!");

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.MangoMap; //import android.app.Activity; import com.google.android.maps.MapActivity; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.os.Bundle; import android.view.View; import android.widget.ListView; import android.widget.SimpleAdapter; import java.text.Collator; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; //MapView Com import com.google.android.maps.MapView; import com.google.android.maps.MapController; import com.google.android.maps.GeoPoint; //import com.google.android.maps. public class MangoMap extends MapActivity { /** Called when the activity is first created. */ private MapView mpv; private MapController mpc; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //地图 mpv = (MapView) findViewById(R.id.map); mpv.setTraffic(true); mpv.setClickable(true); mpv.setScrollContainer(true); mpc = mpv.getController(); GeoPoint gp = new GeoPoint((int) (23.05320 * 1000000), (int) (113.155550 * 1000000)); //地理坐标 mpc.animateTo(gp); mpc.setZoom(15); //mpc. // } @Override protected boolean isRouteDisplayed() { return false; } } //----------------- <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <!-- <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> --> <com.google.android.maps.MapView android:id="@+id/map" android:layout_width="fill_parent" android:layout_height="fill_parent" android:enabled="true" android:clickable="true" android:apiKey="YOUR API key" /> <Button android:text="@+id/Button01" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout> //--------------------- <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.MangoMap" android:versionCode="1" android:versionName="1.0"> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.INTERNET" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <uses-library android:name="com.google.android.maps" /> <activity android:name=".MangoMap" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="3" /> </manifest>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值