首页 > 代码库 > 多线程取钱
多线程取钱
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(); } } }
多线程取钱
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。