java 并发的map_Java并发锁在Map关键级别上

有作者通过调用putPrice方法来更新价格.读者正在使用getPrice获取最新的价格. hasChangedMethod返回一个布尔值,确定自上次调用getPrice以来价格是否已更改.

我正在寻找最快的解决方案.我正在尝试在关键级别上实现线程安全一致的读/写入地图.

我认为锁定整个地图可能会导致一个性能问题,这就是为什么我决定把它放在关键的水平上.不幸的是,它不按预期工作,并阻止整个地图.为什么?你能帮我找出我在这里做错什么吗?

更新:

我想我们可以总结两个问题:1.如果有人在更新过程中,我如何提供对其余密钥的免费访问. 2.如何保证我的方法的原子操作,因为它们需要多个操作读/写.例如getPrice() – 获取价格和更新hasChanged标志.

PriceHolder.java

public final class PriceHolder {

private ConcurrentMap prices;

public PriceHolder() {

this.prices = new ConcurrentHashMap<>();

//Receive starting prices..

Price EUR = new Price();

EUR.setHasChangedSinceLastRead(true);

EUR.setPrice(new BigDecimal(0));

Price USD = new Price();

USD.setHasChangedSinceLastRead(true);

USD.setPrice(new BigDecimal(0));

this.prices.put("EUR", EUR);

this.prices.put("USD", USD);

}

/** Called when a price ‘p’ is received for an entity ‘e’ */

public void putPrice(

String e,

BigDecimal p) throws InterruptedException {

synchronized (prices.get(e)) {

Price currentPrice = prices.get(e);

if (currentPrice != null && !currentPrice.getPrice().equals(p)) {

currentPrice.setHasChangedSinceLastRead(true);

currentPrice.setPrice(p);

} else {

Price newPrice = new Price();

newPrice.setHasChangedSinceLastRead(true);

newPrice.setPrice(p);

prices.put(e, newPrice);

}

}

}

/** Called to get the latest price for entity ‘e’ */

public BigDecimal getPrice(String e) {

Price currentPrice = prices.get(e);

if(currentPrice != null){

synchronized (prices.get(e)){

currentPrice.setHasChangedSinceLastRead(false);

prices.put(e, currentPrice);

}

return currentPrice.getPrice();

}

return null;

}

/**

* Called to determine if the price for entity ‘e’ has

* changed since the last call to getPrice(e).

*/

public boolean hasPriceChanged(String e) {

synchronized (prices.get(e)){

return prices.get(e) != null ? prices.get(e).isHasChangedSinceLastRead() : false;

}

}

}

Price.java

public class Price {

private BigDecimal price;

public boolean isHasChangedSinceLastRead() {

return hasChangedSinceLastRead;

}

public void setHasChangedSinceLastRead(boolean hasChangedSinceLastRead) {

this.hasChangedSinceLastRead = hasChangedSinceLastRead;

}

public BigDecimal getPrice() {

return price;

}

public void setPrice(BigDecimal price) {

this.price = price;

}

private boolean hasChangedSinceLastRead = false;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值