zoukankan      html  css  js  c++  java
  • 多线程取钱

    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();
    		}
    	}
    }
    

      

     

     

     

  • 相关阅读:
    docker删除容器再删除镜像
    centOS7安装docker遇到 [Errno 14] curl#35
    设置centos7界面语言为中文
    sublime查看项目代码多少行
    1. 常用及特殊
    7.逆波兰,二叉树三叉树
    6.表单提交,input键盘变搜索,有关自定义属性input操作
    5.字符串的第一次见到的方法
    2.手机上浏览器看控制台的插件
    1. 时间插件
  • 原文地址:https://www.cnblogs.com/bravolove/p/6657939.html
Copyright © 2011-2022 走看看