相关疑难解决方法(0)

为什么这个同步方法不能按预期工作?

我有一个名为"帐户"的班级

public class Account {

    public double balance = 1500;

    public synchronized double withDrawFromPrivateBalance(double a) {
        balance -= a;
        return balance;
    }
}
Run Code Online (Sandbox Code Playgroud)

还有一个名为ATMThread的类

public class ATMThread extends Thread {
    double localBalance = 0;
    Account myTargetAccount;

    public ATMThread(Account a) {
        this.myTargetAccount = a;
    }

    public void run() {
        find();
    }

    private synchronized void find() {
        localBalance = myTargetAccount.balance;
        System.out.println(getName() + ": local balance = " + localBalance);
        localBalance -= 100;
        myTargetAccount.balance =  localBalance;
    }

    public static void main(String[] args) { …
Run Code Online (Sandbox Code Playgroud)

java multithreading synchronized

6
推荐指数
2
解决办法
1万
查看次数

标签 统计

java ×1

multithreading ×1

synchronized ×1