日期:2014-05-20 浏览次数:20819 次
/** * */ package thread; /** * @description 线程同步 * */ public class SynchronizedTest { public static void main(String[] args) { Account account = new Account("10011",0); new DepositThread2("存钱线程",account,300).start(); new DrawThread2("取钱线程",account,300).start(); } } /** * @description 账户-存/取 * */ class Account2{ private String accountNo; private double balance;//余额 private boolean flag = false;//true代表可取 ,false代表可存 public Account2(String accountNo,double balance) { this.accountNo = accountNo; this.balance = balance; } /** * @return the accountNo */ public String getAccountNo() { return accountNo; } /** * @param accountNo the accountNo to set */ public void setAccountNo(String accountNo) { this.accountNo = accountNo; } /** * @return the balance */ public double getBalance() { return balance; } /** * @description 取钱 * @param drawAmount */ public synchronized void draw(double drawAmount){ try{ if(flag){//flag=true , 表示账户还有钱,等待将钱取走 System.out.println(Thread.currentThread().getName()+" 取钱:"+drawAmount); balance -= drawAmount; System.out.println("账户余额:"+balance); flag = false;//可以存钱 notifyAll(); }else{ wait(); } }catch(Exception e){ } } /** * @description 存钱 * @param drawAmount */ public synchronized void deposit(double depositAmount){ try{ if(flag){//flag=true , 表示账户还有钱,等待将钱取走 wait(); }else{ System.out.println(Thread.currentThread().getName()+" 存钱:"+depositAmount); balance += depositAmount; System.out.println("账户余额:"+balance); flag = true;//可以取钱 notifyAll(); } } catch (InterruptedException e) { } } } /** * @decription 存钱线程 * */ class DepositThread2 extends Thread{ private Account account; private double drawAmount=300; public DepositThread2(String name,Account account,double drawAmount) { super(name); this.account = account; this.drawAmount = drawAmount; } /* (non-Javadoc) * @see java.lang.Thread#run() */ @Override public void run() { for(int i=0;i<5;i++){ account.deposit(drawAmount); } } } /** * @decription 取钱线程 * */ class DrawThread2 extends Thread{ private Account account; private double drawAmount = 300; public DrawThread2(String name,Account account,double drawAmount) { super(name); this.account = account; this.drawAmount = drawAmount; } /* (non-Javadoc) * @see java.lang.Thread#run() */ @Override public void run() { for(int i=0;i<5;i++){ account.draw(drawAmount); } } }