简体   繁体   English

确保并发对象调用而不锁定的最佳方法是什么

[英]What is the best way to ensure concurrent object calls without locking

I have the following objects: 我有以下对象:

// this class is immutable, acts like container for several properties.
public class MyDataAddOps{
    private final boolean isActive;
    private final Map<String,Object> additionalProps;

    public MyDataAddOps(boolean isActive, Map<String,Object> additionalProps){
       this.isActive = isActive;
       this.additionalProps = additionalProps;
    }

   public boolean isActive(){return isActive;}
   public Map<String,Object> getAdditionalProps(){ return additionalProps;}
}

// this class acts as "spring" bean that calls load on construction,
//  and then another scheduler bean calls the load per some cron expression (once a minute for example) 
public class MyDataAddOpsService{
   private MyDataAddOps data;

   // this method will be executed periodically outside
   // via some spring quartz for example
   // the quartz is not re-entrant  
   public void load(){
      // opens some defined file and returns content string 
      String fileData = getFileContent(); 
      boolean isActive = getIsActive(fileData);
      Map<String, Object> props = getProps(fileData);
      data = new MyDataAddOps(isActive, props);
   }

  // This method is executed by many workers threads inside the application
  public boolean isActive(){
     return data.isActive();
  }

  public final Map<String, Object> getProps(){
      return data.getAdditionalProps();
  } 
 }

This approach probably has a race condition where one thread executes isActive() and another load() . 这种方法可能具有一个竞争条件,其中一个线程执行isActive()而另一线程执行load() Although it operates on reference and the object state is not changed. 尽管它在参考上运行并且对象状态未更改。

What is the best solution to support such concurrency? 支持此类并发的最佳解决方案是什么? I would like to avoid syncronized on methods, and also read-write lock. 我想避免方法同步,也避免读写锁定。

Maybe AtomicReference or volatile ? 也许是AtomicReferencevolatile Or maybe it would be better to return only reference to the data itself without proxy methods? 还是最好只返回对数据本身的引用而没有代理方法呢? So no need for locking at all, and all the usage logic is outside this service? 因此,根本不需要锁定,并且所有使用逻辑都在此服务之外吗?

 public class MyDataAddOpsService{
   private MyDataAddOps data;
   public void load(){
     ....
     data = new MyDataAddOps(isActive, props);
   }

 public MyDataAddOps getData(){
    return data;
  }
 }

Your code has yet to grow towards having a race condition; 您的代码还没有朝着产生竞争条件的方向发展。 currently it contains something much more severe, which is a data race . 当前它包含更严重的东西,这是一场数据竞赛 Publishing a reference across threads without inducing a happens before relationship between the write and the future reads means that the reader can see the data object in a partially initialized, inconsistent state. 跨线程发布引用而不会在写入和将来读取之间的关系发生之前发生 ,这意味着读取器可以看到处于部分初始化的不一致状态的数据对象。 Your proposal of a solution does not help with that. 您提出的解决方案对此无济于事。

Once you make the data field volatile , only then will you have a race condition between one thread first reading the data reference, then another thread updating the data reference, then the first thread reading isActive from the old data. 一旦你的data字段中volatile ,只有这样 ,你之间的一个线程首先阅读的竞争条件data引用,那么另一个线程更新data的参考,那么第一个线程读取isActive从旧数据。 This may actually be a benign case for your logic. 对于您的逻辑而言,这实际上可能是一个良性案例。

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

相关问题 在没有 Java Object class 的情况下提取值的最佳方法是什么 - What is the best way to extract a value without having a Java Object class 在JSP中并行化调用的最佳方法是什么? - What's the best way to parallelize calls in JSP? 确保在StringTemplate中转义HTML实体的最佳方法是什么 - What is the best way to ensure HTML entities are escaped in StringTemplate 确保所有输入都是唯一数字的最佳方法是什么 - What is the best way to ensure that all inputs are all unique Numbers 确保并发Web服务调用之间的数据一致性的最佳方法? - Best performing way to guarantee data consistency between concurrent web service calls? 在Java EE应用程序中同时进行httpurlconnection调用的最佳方法是什么 - What is the best way to make simultaneous httpurlconnection calls in a Java EE application 在 Java 8 中设计/处理不同方法的有序调用的最佳方法是什么 - What is the best way to design/process in Java 8 ordered calls on different methods 如何在没有锁定的情况下同步对象? - How synchronize on object without locking? 没有HATEOAS,获得响应的最佳方法是什么? - What is the best way to get response without HATEOAS? 根据外部调用构造域对象的最佳实践是什么? - What is the best practice to construct a Domain Object depending on external calls?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM