gpt4 book ai didi

java - 并发HashMap : Can we trust on it?

转载 作者:行者123 更新时间:2023-12-01 06:56:07 26 4
gpt4 key购买 nike

来自ConcurrentHashMap的文档:

A hash table supporting full concurrency of retrievals and adjustable expected concurrency for updates.

我们能否完全相信ConcurrentHashMap线程安全操作吗?

我正在使用 ConcurrentHashMap 将键与其值进行映射。我的键值对是:

Map<Integer,ArrayList<Double>> map1 = new ConcurrentHashMap();

key 的大小范围为[0,1000000]。我有 20 个线程,可以一次访问/修改与一个键对应的值。这种情况并不常见,但这种情况是可能的。我是通过以下方法获得无穷大:

Double sum =0.0; 
sum = sum + Math.exp(getScore(contextFeatureVector,entry.getValue())+constant);

contextFeatureVectorentry.getValue()是与键关联的数组列表。

[编辑]

 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<> and <code>weightVector</code> looks like



[-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 返回的值不会异常太大。它将在几千人。

最佳答案

它是线程安全的,但可以以非线程安全的方式使用它。

我怀疑您没有对问题进行足够的调查,无法确定已经使用了十多年的 JDK 库中存在错误。

关于java - 并发HashMap : Can we trust on it?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11599949/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com