critical section synchronized 锁住不同对象

本文探讨了在Java并发编程中CriticalSection的概念及其优化方法。通过对比两种不同锁实现方式,详细解释了如何利用synchronized关键字锁住不同对象来提高多线程环境下的并发效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://tutorials.jenkov.com/java-concurrency/race-conditions-and-critical-sections.html

public class TwoSums {
    
    private int sum1 = 0;
    private int sum2 = 0;
    
    public void add(int val1, int val2){
        synchronized(this){
            this.sum1 += val1;   
            this.sum2 += val2;
        }
    }
}
public class TwoSums {
    
    private int sum1 = 0;
    private int sum2 = 0;

    private Integer sum1Lock = new Integer(1);
    private Integer sum2Lock = new Integer(2);

    public void add(int val1, int val2){
        synchronized(this.sum1Lock){
            this.sum1 += val1;   
        }
        synchronized(this.sum2Lock){
            this.sum2 += val2;
        }
    }
}

当两个Critical Section 没有相关性依赖时,可以使用synchronized锁住两个不同的锁来提高并发效率。

boolean isIndexDataFromSettingsAndOtherSuccess() { 1. thread1_checks_field: Thread1 uses the value read from static field mCollector in the condition com.android.settings.intelligence.search.indexing.DatabaseIndexingManager.mCollector == null. It sees that the condition is true. CID 205394: (#1 of 1): Check of thread-shared field evades lock acquisition (LOCK_EVASION) 5. thread2_checks_field_early: Thread2 checks mCollector, reading it after Thread1 assigns to mCollector but before some of the correlated field assignments can occur. It sees the condition com.android.settings.intelligence.search.indexing.DatabaseIndexingManager.mCollector == null as being false. It continues on before the critical section has completed, and can read data changed by that critical section while it is in an inconsistent state. Remove this outer, unlocked check of mCollector. 199 if (mCollector == null) { 2. thread1_acquires_lock: Thread1 acquires lock com.android.settings.intelligence.search.indexing.DatabaseIndexingManager.INDEX_SEARCH_OBJECT. 200 synchronized (INDEX_SEARCH_OBJECT) { 3. thread1_double_checks_field: Thread1 double checks the static field mCollector in the condition com.android.settings.intelligence.search.indexing.DatabaseIndexingManager.mCollector == null. 201 if (mCollector == null) { 4. thread1_modifies_field: Thread1 modifies the static field mCollector. This modification can be re-ordered with other correlated field assignments within this critical section at runtime. Thus, checking the value of mCollector is not an adequate test that the critical section has completed unless the guarding lock is held while checking. If mCollector is assigned a newly constructed value, note that the JVM is allowed to reorder the assignment of the new reference to mCollector before any field assignments that may occur in the constructor. Control is switched to Thread2.
最新发布
06-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值