测试主线程,MapFetch.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
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 +
" 个线程执行完毕!"
);
}
}
|
本文转自韩立伟 51CTO博客,原文链接:http://blog.51cto.com/hanchaohan/1680036,如需转载请自行联系原作者