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锁住两个不同的锁来提高并发效率。