多线程取钱

public class DrawDemo {

	public static void main(String[] args) {

		Account account = new Account("12347866", 1000);
		DrawThread d = new DrawThread(account, "张三", 800);
		DrawThread d1 = new DrawThread(account, "李四", 100);
		DrawThread d2= new DrawThread(account, "王五", 400);
		d.start();
		d1.start();
		d2.start();
	}

}

 

//2.取现的线程
public class DrawThread extends Thread {

	private  Account account;
	private double drawAmount;
	
	public DrawThread(Account account, String name, double drawAmount){
		super(name);
		this.account = account;
		this.drawAmount = drawAmount;
	}
	
	@Override
	public void run() {
		synchronized (account) {
			if(account.getBalance() > drawAmount  && account.getBalance() >= 0) {
				System.out.println("余额足够, "+ Thread.currentThread().getName()+"取出"+ drawAmount + "元");
				try{
					Thread.sleep(1000);
				}catch(Exception e){
					e.printStackTrace();
				}
				
				account.setBalance((account.getBalance() - drawAmount));
				System.out.println("余额还剩:" + (account.getBalance()));
			}else{
				System.out.println("余额不足,取钱失败!!!");
			}
		}
	}
}

 

//3. 实体类
public class Account {
	
	private String accountNo;
	private double balance;
	
	public Account(){}
	public Account(String accountNo, double balance){
		this.accountNo = accountNo;
		this.balance = balance;
	}
	public String getAccountNo() {
		return accountNo;
	}
	public void setAccountNo(String accountNo) {
		this.accountNo = accountNo;
	}
	public double getBalance() {
		return balance;
	}
	public void setBalance(double balance) {
		this.balance = balance;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((accountNo == null) ? 0 : accountNo.hashCode());
		long temp;
		temp = Double.doubleToLongBits(balance);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Account other = (Account) obj;
		if (accountNo == null) {
			if (other.accountNo != null)
				return false;
		} else if (!accountNo.equals(other.accountNo))
			return false;
		if (Double.doubleToLongBits(balance) != Double
				.doubleToLongBits(other.balance))
			return false;
		return true;
	}

 第二种方式: 控制共享资源

//1. 实体类中做文章, 做成同步的方法
public class Account {
	
	private String accountNo;
	private double balance;
	
	public Account(){}
	public Account(String accountNo, double balance){
		this.accountNo = accountNo;
		this.balance = balance;
	}
	public String getAccountNo() {
		return accountNo;
	}
	public void setAccountNo(String accountNo) {
		this.accountNo = accountNo;
	}
	public double getBalance() {
		return balance;
	}
	public void setBalance(double balance) {
		this.balance = balance;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((accountNo == null) ? 0 : accountNo.hashCode());
		long temp;
		temp = Double.doubleToLongBits(balance);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Account other = (Account) obj;
		if (accountNo == null) {
			if (other.accountNo != null)
				return false;
		} else if (!accountNo.equals(other.accountNo))
			return false;
		if (Double.doubleToLongBits(balance) != Double
				.doubleToLongBits(other.balance))
			return false;
		return true;
	}

	public synchronized void drawMoney(double drawAmount){
		System.out.println(Thread.currentThread().getName()+ "客户来了....想取"+drawAmount);
		if(balance - drawAmount > 0){
			System.out.println(Thread.currentThread().getName()+"取出:" + drawAmount);
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			balance -= drawAmount;
			System.out.println("还剩:" + balance);
			
		} else{
			System.out.println("余额不足取现失败");
		}
	}
}


//2. 取现线程
public class DrawThread extends Thread {

	private  Account account;
	private double drawAmount;
	
	public DrawThread(Account account, String name, double drawAmount){
		super(name);
		this.account = account;
		this.drawAmount = drawAmount;
	}
	
	@Override
	public void run() {
		account.drawMoney(drawAmount);
	}
}

//3.测试
public class DrawDemo {

	public static void main(String[] args) {

		Account account = new Account("12347866", 1000);
		DrawThread d = new DrawThread(account, "张三", 800);
		DrawThread d1 = new DrawThread(account, "李四", 100);
		DrawThread d2= new DrawThread(account, "王五", 400);
		d.start();
		d1.start();
		d2.start();
	}
}

  //第三种方式, 改写成实现Runnable接口

//1. 测试开始
public class DrawDemo {

	public static void main(String[] args) {

		Account account = new Account("12347866", 1000);
		DrawThread d = new DrawThread(account, 800);
		DrawThread d1 = new DrawThread(account, 200);
		DrawThread d2 = new DrawThread(account, 100);
		
		Thread t1 = new Thread(d, "孙悟空");
		Thread t2 = new Thread(d1, "猪八戒");
		Thread t3 = new Thread(d2, "小白龙");
		
		t1.start();
		t2.start();
		t3.start();
	
	}
}

//2. 取现线程
public class DrawThread implements Runnable {

	private  Account account;
	private double drawAmount;
	
	public DrawThread(Account account,  double drawAmount){
		this.account = account;
		this.drawAmount = drawAmount;
	}
	
	@Override
	public void run() {
		synchronized (account) {
			if(account.getBalance() > drawAmount  && account.getBalance() >= 0) {
				System.out.println("余额足够, "+ Thread.currentThread().getName()+"取出"+ drawAmount + "元");
				try{
					Thread.sleep(1000);
				}catch(Exception e){
					e.printStackTrace();
				}
				
				account.setBalance((account.getBalance() - drawAmount));
				System.out.println("余额还剩:" + (account.getBalance()));
			}else{
				System.out.println("余额不足,取钱失败!!!");
			}
		}
//		account.drawMoney(drawAmount);
	}
}

//3.实体类--共享资源
public class Account {
	
	private String accountNo;
	private double balance;
	
	public Account(){}
	public Account(String accountNo, double balance){
		this.accountNo = accountNo;
		this.balance = balance;
	}
	public String getAccountNo() {
		return accountNo;
	}
	public void setAccountNo(String accountNo) {
		this.accountNo = accountNo;
	}
	public double getBalance() {
		return balance;
	}
	public void setBalance(double balance) {
		this.balance = balance;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((accountNo == null) ? 0 : accountNo.hashCode());
		long temp;
		temp = Double.doubleToLongBits(balance);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Account other = (Account) obj;
		if (accountNo == null) {
			if (other.accountNo != null)
				return false;
		} else if (!accountNo.equals(other.accountNo))
			return false;
		if (Double.doubleToLongBits(balance) != Double
				.doubleToLongBits(other.balance))
			return false;
		return true;
	}

//	public synchronized void drawMoney(double drawAmount){
//		System.out.println(Thread.currentThread().getName()+ "客户来了....想取"+drawAmount);
//		if(balance - drawAmount > 0){
//			System.out.println(Thread.currentThread().getName()+"取出:" + drawAmount);
//			try {
//				Thread.sleep(1000);
//			} catch (InterruptedException e) {
//				e.printStackTrace();
//			}
//			balance -= drawAmount;
//			System.out.println("还剩:" + balance);
//			
//		} else{
//			System.out.println("余额不足取现失败");
//		}
//	}
}

  第四种: 通过ReentranLock来同步

//其他地方同前面一样
package com.ckang.drawmoney;

import java.util.concurrent.locks.ReentrantLock;

public class Account {
	private final ReentrantLock lock = new ReentrantLock();
	private String accountNo;
	private double balance;
	
	public Account(){}
	public Account(String accountNo, double balance){
		this.accountNo = accountNo;
		this.balance = balance;
	}
	public String getAccountNo() {
		return accountNo;
	}
	public void setAccountNo(String accountNo) {
		this.accountNo = accountNo;
	}
	public double getBalance() {
		return balance;
	}
	public void setBalance(double balance) {
		this.balance = balance;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((accountNo == null) ? 0 : accountNo.hashCode());
		long temp;
		temp = Double.doubleToLongBits(balance);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Account other = (Account) obj;
		if (accountNo == null) {
			if (other.accountNo != null)
				return false;
		} else if (!accountNo.equals(other.accountNo))
			return false;
		if (Double.doubleToLongBits(balance) != Double
				.doubleToLongBits(other.balance))
			return false;
		return true;
	}

	public void drawMoney(double drawAmount){
		lock.lock();
		try{
			System.out.println(Thread.currentThread().getName()+ "客户来了....想取"+drawAmount);
			if(balance - drawAmount > 0){
				System.out.println(Thread.currentThread().getName()+"取出:" + drawAmount);
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				balance -= drawAmount;
				System.out.println("还剩:" + balance);
				
			} else{
				System.out.println("余额不足取现失败");
			}
		}finally{
			lock.unlock();
		}
	}
}

  

 

 

 

posted @ 2017-04-01 23:51  黑土白云  阅读(137)  评论(0编辑  收藏  举报