classBankAccount{privateint balance =100;publicintgetBalance(){return balance;}publicvoidwithdraw(int amount){balance = balance - amount;}}publicclassRyanAndMonicaJobimplementsRunnable{private BankAccount account =newBankAccount();publicstaticvoid main (String[] args){RyanAndMonicaJob theJob =newRyanAndMonicaJob();Thread one =newThread(theJob);Thread two =newThread(theJob);one.setName("Ryan");two.setName("Monica");one.start();two.start();}publicvoidrun(){for(int x =0; x <10; x++){makeWithdrawal(10);if(account.getBalance()<0){System.out.println("Overdrawn!");}}}privatevoidmakeWithdrawal(int amount){String currentThread = Thread.currentThread().getName();if(account.getBalance()>= amount){System.out.println(currentThread +"is about to withdraw");try{System.out.println(currentThread +" is going to sleep");Thread.sleep(500);}catch(InterruptedException ex){ex.printStackTrace();}System.out.println(currentThread +" woke up.");account.withdraw(amount);System.out.println(currentThread +" completes the withdrawl");}else{System.out.println("Sorry, not enough for "+ currentThread);}}}
```
第五章第一节 可复用性的度量、形态和外部观察 面向复用编程(programming for reuse):开发出可复用的软件 基于复用编程(programming with reuse):利用已有的可复用软件搭建应用系统 代码复用的类型: 白盒复用:源代码可见&#x…