一次同步方法的失败与原因总结

synchronized关键字可以设置同步方法和同步块,同步方法会对调用对象this上锁,
所有线程进入前都需要获取这个锁,这里我拿一个错误样例来做示范

 1 public class Test {
 2     public static void main(String[] args) {
 3         Account account=new Account(100);
 4         Person p1=new Person(account,80);
 5         Person p2=new Person(account,90);
 6         p1.start();
 7         p2.start();
 8     }    
 9 }
10 
11 class Account{
12     int total;    
13     public Account(int total) {
14         super();
15         this.total=total;        
16     }    
17 }
18 
19 class Person extends Thread{
20     private int reduce;
21     private Account account;
22     public Person(Account account,int reduce) {
23     
24         this.account=account;
25         this.reduce=reduce;
26     }
27     
28     public synchronized void run() {    
29         if (account.total-reduce<0) return ;
30         try {
31             Thread.sleep(200);
32         } catch (InterruptedException e) {
33             // TODO Auto-generated catch block
34             e.printStackTrace();
35         }
36         account.total-=reduce;
37         System.out.println(Thread.currentThread().getName()+"取出"+reduce);
38         System.out.println(Thread.currentThread().getName()+"剩余"+account.total);
39         
40     }
41 }


Thread-0取出80
Thread-0剩余-70
Thread-1取出90
Thread-1剩余-70

出现了负数,很明显没锁住,现在我们来分析下原因。
有1个account对象,两个person对象p1和p2,p1和p2争抢资源account
我一开始的设想是,当p1进入方法时,p1对象上锁,account作为p1的成员也被上锁,此时p2进入就需要等到p1释放锁,由此达到我们的目标。

问题在于:java规定每个对象都有一个监视器,使用时查看上锁了没,上面的代码中虽然对person对象上了锁,同时account作为对象也有监视器,account虽然作为person的成员,但account的监视器是单独的,不受p1的锁影响。现在我们再来场景复原一下,p1先进入方法,对p1上锁,计算100-80=20,同时p2进入方法,查看account的监视器,没有上锁,计算20-90=-70,这就产生了错误。

解决方法就是使用同步块给account上锁

 1 public void run() {    
 2         synchronized(account) {
 3         if (account.total-reduce<0) return ;
 4         try {
 5             Thread.sleep(200);
 6         } catch (InterruptedException e) {
 7             // TODO Auto-generated catch block
 8             e.printStackTrace();
 9         }
10         account.total-=reduce;
11         System.out.println(Thread.currentThread().getName()+"取出"+reduce);
12         System.out.println(Thread.currentThread().getName()+"剩余"+account.total);
13         
14         }
15     }


这个问题给我们的教训在于,每个对象都有单独的监视器,要选取正确的争夺对象上锁,基于此同步块不仅能缩小锁的粒度还能选取正确的对象上锁。

猜你喜欢

转载自www.cnblogs.com/jchen104/p/10178568.html
今日推荐