java线程学习5——线程同步之同步方法

来源:互联网 发布:石家庄seo外包 编辑:程序博客网 时间:2024/05/16 06:08

 

public class Account
{
 /**
  * 账户号
  */
 private String accountNo;
 /**
  * 账户余额
  */
 private double balance;

 public Account()
 {
  super();
 }

 public Account(String accountNo, double balance)
 {
  super();
  this.accountNo = accountNo;
  this.balance = balance;
 }

 @Override
 public int hashCode()
 {
  return accountNo.hashCode();
 }

 @Override
 public boolean equals(Object obj)
 {
  if (null != obj && obj.getClass() == Account.class)
  {
   Account target = (Account) obj;
   return target.accountNo.equals(accountNo);
  }
  return false;
 }

 /**
  * 同步方法,同步方法的监视器是this,同步方法可以将该类变为线程安全的类
  * 任何时候只有一个线程获得对Account对象的锁定,然后进入draw方法进行取钱
  */
 public synchronized void draw(double drawAmount)
 {
  if (balance >= drawAmount)
  {
   System.out.println(Thread.currentThread().getName() + "取出钞票成功" + drawAmount);
   balance -= drawAmount;
   System.out.println("余额为" + balance);
  }
  else
  {
   System.out.println("余额不足");
  }
 }

 /********************** 只提供Getters,不提供Setters,确保安全 ************************/

 public String getAccountNo()
 {
  return accountNo;
 }

 public double getBalance()
 {
  return balance;
 }
}

 

 

public class DrawThread extends Thread
{
 /**
  * 模拟账户
  */
 private Account ac;

 /**
  * 当前取钱线程希望取得的钱数
  */
 private double drawAmount;


 public DrawThread(String name, Account ac, double drawAmount)
 {
  super(name);
  this.ac = ac;
  this.drawAmount = drawAmount;
 }

 @Override
 public void run()
 {
  ac.draw(drawAmount);
 }
}

 

public class Test
{
 public static void main(String[] args)
 {
  Account ac = new Account("00000001", 1000);
  Thread t1 = new Thread(new DrawThread("Lily", ac, 800));
  Thread t2 = new Thread(new DrawThread("Tom", ac, 800));
  t1.start();
  t2.start();
 }
}