在并发环境中,只从map中获取对象,没有put。 HashMap
和ConcurrentHashMap
,在jdk 1.6中表现更好。
根据我对这两张地图的有限经验,ConcurrentHashMap
由细分组成。当从中获取对象时,它至少需要两个定位动作。首先是找到正确的segment
,然后找到正确的table
,而HashMap
只需要找到一次。而且,在我看来,当我们从地图中获取对象时,没有锁定种族。所以我认为在jdk 1.6中,毫无疑问HashMap
在并发环境中从ConcurrentHashMap
获得对象时会比public static void main(String[] args) throws InterruptedException {
final Map<Integer, Integer> hashMap = new HashMap<Integer, Integer>();
final Map<Integer, Integer> concurrentHashMap = new ConcurrentHashMap<Integer, Integer>();
init(hashMap);
init(concurrentHashMap);
final Random random = new Random(System.currentTimeMillis());
final CountDownLatch latch1 = new CountDownLatch(100);
long start1 = System.nanoTime();
for (int i = 0; i < 100; i++) {
new Thread(new Runnable() {
@Override
public void run() {
for (int count = 0; count < 10000; count++) {
hashMap.get(random.nextInt(100));
}
latch1.countDown();
}
}).start();
}
latch1.await();
long end1 = System.nanoTime();
System.out.println("hashMap : " + (end1 - start1));
final CountDownLatch count2 = new CountDownLatch(100);
long start2 = System.nanoTime();
for (int i = 0; i < 100; i++) {
new Thread(new Runnable() {
@Override
public void run() {
for (int count = 0; count < 10000; count++) {
concurrentHashMap.get(random.nextInt(100));
}
count2.countDown();
}
}).start();
}
count2.await();
long end2 = System.nanoTime();
System.out.println("concurrentHashMap : " + (end2 - start2));
}
private static void init(Map<Integer, Integer> map) {
for (int i = 0; i < 100; i++) {
map.put(i, i);
}
}
更好。
但不幸的是,测试结果并不支持我的观点。
这是我的测试代码。
hashMap : 78448168
concurrentHashMap : 68820282
hashMap : 87819463
concurrentHashMap : 76098734
hashMap : 87901572
concurrentHashMap : 71385562
最后三个输出是:
HashMap
然后我在单线程环境中测试性能。(从地图重复随机获取对象10000次。)
这是我的测试结果。
ConcurrentHashMap
的平均时间为4982047 ns,而ConcurrentHashMap
的平均时间为3150367 ns。
结果表明HashMap
性能优于from random import randint
def print_scores(uscore, cscore):
uscore = 0
cscore = 0
print(uscore, cscore)
def roll_two_dice():
roll1 = randint(1, 6)
roll2 = randint(1, 6)
if roll1 or roll2 == 1:
return 0
elif roll1 and roll2 == 1:
return 25
else:
return roll1 + roll2
def computerTurn(score):
turn_score = 0
while score + turn_score < 100 and turn_score < 20:
roll = roll_two_dice()
if roll == 0:
return 0
score = roll + turn_score
return turn_score
def userTurn(score):
turn_score = 0
while score + turn_score < 100 or user == hold:
roll = roll_two_dice()
if roll == 0:
return 0
score = roll + turn_score
return turn_score
def final_result(uscore, cscore):
uscore = 0
cscore = 0
print('Final results:{}'.format(uscore, cscore))
if uscore >= 100:
print('You win')
else:
print('Computer wins')
def main():
c_score = 0
u_score = 0
if randint(0, 1) == 0:
print('Computer starts')
c_turn = computerTurn(c_score) + c_score
while c_score < 100:
print('Current scores')
print_scores(u_score, c_score)
u_turn = userTurn(u_score) + u_score
if u_score >= 100:
exit
c_turn = computerTurn(c_score)
print(c_turn)
c_score = c_score + c_turn
final_result(u_score, c_score)
main()
,即使在单线程环境中也是如此。
我真的很困惑。我有什么理解错了吗?或者只是我的测试代码不严谨?