ConcurrentHashMap:我们可以信任它吗? [英] ConcurrentHashMap: Can we trust on it?

查看:30
本文介绍了ConcurrentHashMap:我们可以信任它吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 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 来映射键和它们的值.我的键值对是:

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);

contextFeatureVectorentry.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<> 和 weightVector 看起来都像

[ - 0.005554038592516575,0.0048966974158881175,-0.05315976588195846,-0.030837804373964654,0.014483064988148562,-0.018962129117649,-0.015221386014208877,0.015825702365331477,-0.11363620479662287,0.00802609847263844,-0.062106636476812194,0.0081088544712​​93185,-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]

[-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.

推荐答案

您使用的数据结构让我相信您的代码中一定存在一些错误.您很可能正在从地图中获取列表并对其进行更新:

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);

请注意,add(5)不是线程安全的,因为它对普通的 ArrayList 进行操作.您需要线程安全的 ArrayListreplace(K key, 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆