猿问

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

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取出80
Thread-0剩余-70
Thread-1取出90
Thread-1剩余-70

这里是模拟的从账户取钱的操作,是用的同步方法,但是这里锁失败了。我看别人说是account没锁住,但是
account也是person对象的成员啊,同步方法不是把person对象锁住了吗,为什么这里没上锁呢?

精慕HU
浏览 659回答 2
2回答

不负相思意

已经解决了,每个对象都有一个监视器查看是否上锁,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); } }

HUWWW

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

相关分类

Java
我要回答