繁体   English   中英

如何为有意义的相等对象创建线程锁/同步并防止各个线程的并行执行?

[英]How can I create a thread lock/synchronization for meaningfully equal objects and prevent parallel execution of the respective threads?

private Map<CustomerKey, Customer> customerMap = new ConcurrentHashMap<CustomerKey, Customer>();

public Customer getCustomer(CustomerKey customerKey)
{   
    Customer customer = customerMap.get(customerKey);
    if(null == customer)
    {
        synchronized(this)
        {
            customer = customerMap.get(customerKey); // Added line
            if(null == customer)
            {
                customer = new Customer();
                customerMap.put(customerKey, customer); // Added line
            }
        }
    }
    return customer;
}

这就是我们通常进行对象级锁定的方式。

在此示例中,无论 customerKey 对象的值如何,都会应用对象级锁定。 因此,即使对于不同的 customerKey 对象,特定块也会同步。 我不想要这种行为。

而不是“这个”变量,如果我获取像下面这样的 customerKey 对象的锁,

同步(客户密钥)

多个线程可以传递不同的 customerKey 对象但具有相同的值,这意味着它们有意义地相等 (customerKeyThread1.equals(customerKeyThread2)) 但不相同的对象 (customerKeyThread1 != customerKeyThread2)

所以锁定 customerKey 对象也不是一个有效的解决方案。

所以我需要一些逻辑来为一组代码提供同步,不仅为同一个对象,而且为有意义的相等对象 我怎样才能达到同样的目标?

那么你在这种情况下使它更简单(在我看来),只需使用ConcurrentHashMap.computeIfAbsent因为它记录在案:

与指定键关联的当前(现有或计算出的)值,如果计算出的值为空,则为空。

它还说:

整个方法调用以原子方式执行....

所以你可以这样做:

public Customer getCustomer(CustomerKey customerKey) {
    return customerMap.computeIfAbsent(customerKey, x-> new Customer());
}

暂无
暂无

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

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