在Spring服务类中使用ConcurentHashMap和AtomicInteger作为实例变量 [英] Using ConcurentHashMap and AtomicInteger as instance variables within a spring service class

查看:605
本文介绍了在Spring服务类中使用ConcurentHashMap和AtomicInteger作为实例变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  @Service,我试图在注入到控制器中的单例弹簧服务中实现ConcurentHashMap的线程安全使用
public MyService {

final ConcurrentMap< String,AtomicInteger> myMap = new ConcurrentHashMap< String,AtomicInteger>(10){
{/ *初始化十个键/值* /
}
}

public int add(String key){
return myMap.get(key).incrementAndGet();
}

//通过ajax循环(和控制器)访问,如果值更改显示
public int getCount(String key){
return myMap.get key).get();
}
}

有更好的方法来访问hashmap线程安全?我如何适应这在集群环境中工作?这是一个由我的其他问题。

解决方案

我的目标不是有问题esp的答案。因为我没有集群情况的专业知识;但是,我想指出我认为值得注意的。



@JB Nizet在其中一个注释中提到该代码是线程安全的和更正。我想根据不一致。 htmlrel =nofollow> Java API参考


检索操作(包括get)一般不会阻塞,因此可能
与更新操作(包括put和remove)重叠


这意味着可能有客户端获取此信息某些更新目前正在运行。而且,这是有道理的,因为你在代码中提到循环。因此,如果这是在您的情况下很重要,那么一切都应该很好。



但是,如果你需要使这个更严格,我想可能使用 ReentrantReadWriteLock 将是一个不错的选择。此锁定可让您的代码阻止所有读取请求,直到有可用信息的一致快照。你可能使用 getCount 方法的锁严格阻塞,直到 add 方法释放所有等待的锁在代码中使用的映射的一致性快照。



我也有一个猜测,同样的问题是有效的,当你迁移到一个群集解决方案;如果在不同的群集节点之间需要一致性,那么应该考虑它。


I am attempting to implement thread-safe usage of ConcurentHashMap within singleton spring service that is injected into controllers:

@Service
public MyService{

  final ConcurrentMap<String, AtomicInteger> myMap = new ConcurrentHashMap<String,   AtomicInteger>(10) {
        {/* initialize the ten key/values */
        }
    };

 public int add(String key) {
   return myMap.get(key).incrementAndGet();
 }

    // accessed via ajax loop (and controller), if value changes update display
  public int getCount(String key) {
    return myMap.get(key).get();
  }
}

Is there a better way to make access to a hashmap thread-safe? How could I adapt this to work in a clustered environment? It is a follw up to my other question.

解决方案

I do not aim to have an answer for the question esp. because I do not have expertise in the clustered situation; however, I'd like to point out what I think is worth noticing.

@JB Nizet in one of the comments mentions that the code is thread-safe and correct. I would like to add but not consistent based on Java API Reference:

Retrieval operations (including get) generally do not block, so may overlap with update operations (including put and remove)

It means that there can be a client getting this information while some update is currently running. And, that makes sense since you mention 'looping' in your code. So, if this is NOT important in your case, then everything should be just fine.

But, if you need to make this more strict, I was thinking maybe using an instance of ReentrantReadWriteLock would be a good choice. The lock enables your code to block all the read requests until there is a consistent snapshot of the information available. You'd be probably using the lock on the getCount method to strictly block until add method frees all the locks waiting for the consistent snapshot of the map used in the code.

I also have a guess that the same concern is valid when you migrate this to a clustered solution; if consistency is required across different cluster nodes, then it should be taken care of.

这篇关于在Spring服务类中使用ConcurentHashMap和AtomicInteger作为实例变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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