简体   繁体   English

ConcurrentHashMap值变为空

[英]ConcurrentHashMap values become empty

I have two classes one of which implements runnable, this one gets the data from a website then puts it into a ConcurrentHashMap and the other gets this data later. 我有两个类,其中一个实现可运行的,这一个从网站获取数据,然后将其放入ConcurrentHashMap中,另一个稍后再获取此数据。 First class successfully gets the data and puts it into the map, I can see the map is full but when the other class tries to get data, keys are here but values become empty. 第一类成功获取数据并将其放入地图中,我可以看到地图已满,但是当另一类尝试获取数据时,键在这里,但值变为空。

public class KAuctionThread extends KObject implements Runnable 
{
...
    KParserMapBaseClass.getLiveStreamMap( this.getThreadConfig( ).getDataSource( ) ).put( realTimeActionKey, di ) ;
...
}

This line puts the data into the map below 这行将数据放入下面的地图中

public abstract class KParserMapBaseClass extends KObject implements Callable<KDownloadInfo>
{
public static ConcurrentHashMap<String, ConcurrentHashMap<String, KDownloadInfo>> liveStream = new ConcurrentHashMap<>( ) ;// !!!
...
...
public static ConcurrentHashMap<String, KDownloadInfo> getLiveStreamMap( String dataSource )
{
    ConcurrentHashMap<String, KDownloadInfo> dataSourceMap = KParserMapBaseClass.liveStream.get( dataSource ) ;

    if( dataSourceMap == null )
    {
        dataSourceMap = new ConcurrentHashMap<String, KDownloadInfo>( ) ;

        KParserMapBaseClass.liveStream.put( dataSource, dataSourceMap ) ;
    }

    return dataSourceMap ;
}
}

I can see the data in eclipse expression when first class puts it: 当第一堂课把数据放在eclipse表达式中时,我可以看到它:

{B2BOtoNet={someurl=com.lib1k.cmap.agent2.KDownloadInfo@622d2710}}

But when the other class tries to reach 但是当另一个阶级试图达到

ConcurrentHashMap<String, KDownloadInfo> map = KParserMapBaseClass.getLiveStreamMap( di.dataSource ) ;
    if( map != null && map.size( ) > 0 )
    {
        String key = map.keySet( ).iterator( ).next( ) ;

        KDownloadInfo rtdi = map.remove( key ) ;

        return rtdi ;
    }

Map becomes like that: 地图变成这样:

{B2BOtoNet={}}

There is no other class that puts or gets the data 没有其他可放入或获取数据的类

It's very difficult to say without a reproducible example. 没有可复制的例子很难说。 It could be that this method is not atomic and that you are overriding instances in the map that have already been created before. 可能是此方法不是原子方法,并且您将覆盖映射中之前已创建的实例。

public static ConcurrentHashMap<String, KDownloadInfo> getLiveStreamMap(String dataSource)
{
    ConcurrentHashMap<String, KDownloadInfo> dataSourceMap = KParserMapBaseClass.liveStream.get(dataSource) ;

    if (dataSourceMap == null)
    {
        dataSourceMap = new ConcurrentHashMap<String, KDownloadInfo>();
        KParserMapBaseClass.liveStream.put(dataSource, dataSourceMap);
    }
    return dataSourceMap;
}

You can do the same thing more concisely and atomically: 您可以更简洁,更原子地执行相同的操作:

public static ConcurrentHashMap<String, KDownloadInfo> getLiveStreamMap(String dataSource)
{
    return liveStream.computeIfAbsent(dataSource, k -> new ConcurrentHashMap<>());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM