使用ConcurrentHashMap消除了数据可见性问题? [英] Use of ConcurrentHashMap eliminates data-visibility troubles?

查看:480
本文介绍了使用ConcurrentHashMap消除了数据可见性问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了 Java Concurrency in Practice 并留下了这个问题:当我使用ConcurrentHashMap时,本书第一部分讨论的数据并发问题我还需要担心吗?以下是我的某个程序中的几个示例:

I've read through Java Concurrency in Practice and am left with this question: when I use a ConcurrentHashMap, what data concurrency issues discussed in Part One of the book do I still need to worry about? Here are a couple of examples from one of my programs:

1。交易者的当前位置(共享整数,其中'整数'是数学术语)

1. Current position of a trader (a shared integer where 'integer' is the mathematical term)

此数字表示交易者对象当前拥有并定义其状态。它必须阅读它的位置,知道该做什么(寻找开始一个新的位置,或管理当前的位置)。 Trader 方法在自己的线程上运行。

This number represents what a trader object currently owns and defines its state. It must read its position to know what to do (look to start a new position, or manage the current one). Trader methods run on their own thread.

A 经纪人对象负责设置交易者的头寸。每当交易者的一个订单被填满时,它将设置头寸。 Broker 方法在自己的线程上运行。

A broker object is in charge of setting the trader's position. It will set the position each time one of the trader's orders is filled. Broker methods run on their own thread.

交易者经纪人都在同一个包。 Position实现为package-private static ConcurrentHashMap 。键是交易者对象的id。值为Integer。

Both the trader and broker are in the same package. Position is implemented as a package-private static ConcurrentHashMap. The keys are id's of the trader objects. The values are Integer.

包的外部是应用程序。它通过公共吸气剂间接获得交易者的头寸。

External to the package is the application. It gets the traders' positions indirectly with a public getter.

职位每隔几分钟最多会改变一次,因此经纪人不会经常触及地图。但是,交易者和应用程序将经常读取。此外,我们经常有几个交易者同时阅读地图。

Positions will change at most once every few minutes, so the broker won't touch the map very often. However, the trader and application will frequently read. In addition we often have several traders reading the map concurrently.

因此,以这种方式使用ConcurrentHashMap,我不必处理锁定和数据可见性问题? ConcurrentHashMap可以处理所有事情吗?

So using a ConcurrentHashMap this way, I don't have to work about locking and data visibility? The ConcurrentHashMap takes care of everything?

2。市场(买入价,卖出价,最后价格)

与头寸相同的情况,除了经纪人将非常频繁地更新价格(在繁忙时间每秒最多10次更新;通常是每秒几次)。 交易者和应用程序仍然经常读取。现在,地图键是指示哪个库存或未来的代码,以及值是保持市场价格的对象。

Pretty much the same situation as position, except now the broker will very frequently update the prices (up to 10 updates a second during busy times; normally a few times a second). The trader and application still do frequent reads. The map keys now are codes indicating which stock or future, and the values are objects which hold the market prices.

它似乎工作正常,但在阅读JCIP之后,我意识到如果事情没有正确实施,程序仍然会被破坏。这本书讨论了ConcurrentHashMap,但没有明确告诉我第一部分我们不再需要手动解决的问题。 出现,在这种情况下,我没有 synchronize 。这是正确的吗?

It seems to work okay, but after reading JCIP, I realize the program can still be broken if things are not implemented correctly. The book talks about the ConcurrentHashMap but doesn't explicitly tell me what issues from Part I we no longer have to address manually. It appears that I don't have to synchronize anything in this case. Is that correct?

推荐答案


所以以这种方式使用ConcurrentHashMap,
我不喜欢我必须解决锁定和
数据可见性? ConcurrentHashMap
会处理所有事情吗?

So using a ConcurrentHashMap this way, I don't have to work about locking and data visibility? The ConcurrentHashMap takes care of everything?

这取决于Map中的内容,如果我正确阅读了你的例子情况看起来像这样

This depends on what is in the Map, if I read your example correctly the situation looks like this

static final ConcurrentMap<Integer,Integer> map = ...

class Trader{

  public int doRead(){
      map.get(someId);
   }
}
class Broker{
   public void doWrite(){
      map.put(someId,someValue);
   }
}

如果是这样的话,那就是所有并发被照顾。

If that is the case, then yes all the concurrency is taken care of.

但是如果地图看起来像

static final ConcurrentMap<Integer,Trader> map = ..

    class Broker{
       public void doWrite(){
          map.get(someId).setPosition(somePosition);
       }
    }

即使ConcurrentHashMap锁定,这也不是线程安全的放置时,此时对象的所有并发访问必须处理自己的同步。

This is NOT thread safe, even though the ConcurrentHashMap locks when you put, all concurrent access of objects at this point must handle their own synchronization.

这篇关于使用ConcurrentHashMap消除了数据可见性问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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