首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

线程同步的经典事例

2012-11-20 
线程同步的经典例子银行存钱与取钱的例子能够很好的说明线程同步的概念一:首先定义帐号类,其中有一个余额

线程同步的经典例子

银行存钱与取钱的例子能够很好的说明线程同步的概念

一:首先定义帐号类,其中有一个余额的字段:

?

public class BankAccount {private int balance;public BankAccount( int balance) {this.balance = balance;}public int getBalance() {return balance;}/** * 存款 */public synchronized void deposit(int amount) {balance += amount;}/** * 取款 */public synchronized  void withdraw(int amount) {balance -= amount;}}
?

?

热点排行