Java 6-2 jmu-Java-07多线程-互斥访问 (15分)

定义Account类
属性:
private int balance
方法:
getter方法
void deposit(int money) //存钱,在余额的基础上加上money
void withdraw(int money) //取钱,在余额的基础上减去money

注意:可能有多个线程通过deposit或withdraw方法同时存取Account对象的balance属性。

class Account
{
	private int balance=0;

	public Account(int balance) {
		this.balance = balance;
	}

	public Account() {
		super();
	}

	public int getBalance() {
		return balance;
	}

	public void setBalance(int balance) {
		this.balance = balance;
	}
	public synchronized void deposit(int money) {
		this.balance = balance+money;
	}
	public synchronized void withdraw(int money) {
		this.balance = balance-money;
	}
}

synchronized同步

你可能感兴趣的:(java)