为了账号安全,请及时绑定邮箱和手机立即绑定

synchronized同步方法没锁住成员变量是为什么

synchronized同步方法没锁住成员变量是为什么

精慕HU 2019-01-16 08:54:46
public class Test { public static void main(String[] args) { Account account=new Account(100); Person p1=new Person(account,80); Person p2=new Person(account,90); p1.start(); p2.start(); } } class Account{ int total; public Account(int total) { super(); this.total=total; } } class Person extends Thread{ private int reduce; public Account account; public Person(Account account,int reduce) { this.account=account; this.reduce=reduce; } public synchronized void run() { if (account.total-reduce<0) return ; try { Thread.sleep(200); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } account.total-=reduce; System.out.println(Thread.currentThread().getName()+"取出"+reduce); System.out.println(Thread.currentThread().getName()+"剩余"+account.total); } } Thread-0取出80Thread-0剩余-70Thread-1取出90Thread-1剩余-70 这里是模拟的从账户取钱的操作,是用的同步方法,但是这里锁失败了。我看别人说是account没锁住,但是account也是person对象的成员啊,同步方法不是把person对象锁住了吗,为什么这里没上锁呢?
查看完整描述

2 回答

?
不负相思意

TA贡献1777条经验 获得超10个赞

已经解决了,每个对象都有一个监视器查看是否上锁,person对象有,account对象也有,person对象上锁并不能影响到account的锁,这也就是为什么account没被锁住的原因,p1进入方法后,p2再进入是查看account的监视器,没有锁,成功进入。所以说没有锁住。这里需要的是account的锁。

public void run() {    
        synchronized(account) {
        if (account.total-reduce<0) return ;
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        account.total-=reduce;
        System.out.println(Thread.currentThread().getName()+"取出"+reduce);
        System.out.println(Thread.currentThread().getName()+"剩余"+account.total);
        
        }
    }
查看完整回答
反对 回复 2019-02-12
?
HUWWW

TA贡献1874条经验 获得超12个赞

p1,p2是两个不同的线程对象,他们进入同步方法run()的时候分别拿的是p1, p2各自的锁,所以他们根本没起到同步效果

查看完整回答
反对 回复 2019-02-12
  • 2 回答
  • 0 关注
  • 658 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号