ConcurrentHashMap的:我们能相信它? [英] ConcurrentHashMap: Can we trust on it?
问题描述
从ConcurrentHashMap中的文件:
From the document of ConcurrentHashMap:
一个哈希表支持检索和更新的所期望可调整并发的完全并发。
A hash table supporting full concurrency of retrievals and adjustable expected concurrency for updates.
我们能否完全相信的ConcurrentHashMap
并线程安全的操作?
Can we fully believe that ConcurrentHashMap
does thread safe operation?
我使用的ConcurrentHashMap与他们values.My键值对映射的关键是:
I am using ConcurrentHashMap for mapping key with their values.My key-value pair is:
Map<Integer,ArrayList<Double>> map1 = new ConcurrentHashMap();
键范围从[0,1000000]的大小。我有可以一次访问/修改对应值到钥匙20个线程。这不是那么频繁,但该条件是可能的。我是
从下面的方法获得一个无边:
The size of key ranges from [0,1000000]. I have 20 threads which can access/modify value corresponding to a key at a time. This not so frequent but that condition is possible. I am getting an infinity from following method:
Double sum =0.0;
sum = sum + Math.exp(getScore(contextFeatureVector,entry.getValue())+constant);
contextFeatureVector
和 entry.getValue()
是与键关联数组列表。
contextFeatureVector
and entry.getValue()
are arraylist associated with a key.
constant =0.0001
private double getScore(List<Double> featureVector,List<Double>weightVector) throws NullPointerException
{
double score =0.0;
int length = featureVector.size();
for (int i =0 ; i< length ; i++){
score = score + (featureVector.get(i)*weightVector.get(i));
}
return score;
}
两者 featureVector&LT;>和
weightVector
看起来
[-0.005554038592516575, 0.0048966974158881175, -0.05315976588195846, -0.030837804373964654, 0.014483064988148562, -0.018962129117649, -0.015221386014208877, 0.015825702365331477, -0.11363620479662287, 0.00802609847263844, -0.062106636476812194, 0.008108854471293185, -0.03193255218671684, 0.04949650992670292, -0.0545583154094599, -0.04873314092706468, 0.013534731656877033, 0.08433117163682455, 0.050310355477044114, -0.002420513353516017, -0.02708299928442614, -0.023489187394176294, -0.1277699782685597, -0.10071004855129333, 0.08649040730064464, -0.04940329664431305, -0.027481729446035053, -0.0571846057609884, -0.036738550618481455, -0.035608113682344365]
因此,从getScore返回不下去格外太大的价值。这将是在
几千。
thus the value returned from getScore does not go exceptionally too large. it will be in some thousands.
推荐答案
您使用的数据结构,使我相信一定有你的code一些bug。最有可能你是从地图获取列表,并更新它:
The data structure you use makes me believe there must some bug in your code. Most likely you are fetching the list from map and updating it:
map1.get(42).add(5);
注意添加(5)
是不会线程安全的,因为它在普通操作的ArrayList
。您也需要线程安全的的ArrayList
或<一个href=\"http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ConcurrentHashMap.html#replace%28K,%20V,%20V%29\"相对=nofollow> 更换(K键,V属性oldValue,V newValue)以
方法。
Note that add(5)
is not thread-safe as it operates on ordinary ArrayList
. You either need thread safe ArrayList
or replace(K key, V oldValue, V newValue)
method.
如果您通过的ConcurrentHashMap
正在给担保仔细阅读,就可以有效地使用它。
If you read carefully through the guarantees ConcurrentHashMap
is giving, you can use it effectively.
这篇关于ConcurrentHashMap的:我们能相信它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!