why doesn't this synchronized method work as expected?

后端 未结 3 925
一向
一向 2020-12-20 17:45

I have a class called \"Account\"

public class Account {

    public double balance = 1500;

    public synchronized double withDrawFromPrivateBalance(double         


        
3条回答
  •  春和景丽
    2020-12-20 18:23

    Marking a method synchronized obtains a lock on the object that the method is being run on, but there are two different objects here: the ATMThread and the Account.

    In any event, the two different ATMThreads are using different locks, so their writes can overlap and conflict with one another.

    Instead, you should have both ATMThreads synchronize on the same object.

提交回复
热议问题