Multithreading Take money

Source: Internet
Author: User

public class Drawdemo {public static void main (string[] args) {Account account = new Account ("12347866", "");D Rawthread D = new Drawthread (account, "Zhang San", +);D rawthread d1 = new Drawthread (account, "John Doe", +);D rawthread d2= new Drawthread (AC Count, "Harry",;d. Start ();d 1.start ();d 2.start ();}}

 

2. Withdrawal thread 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;} @Overridepublic void Run () {synchronized (account) {if (Account.getbalance () > Drawamount  && Account.getbalance () >= 0) {System.out.println ("sufficient balance," + thread.currentthread (). GetName () + "Remove" + Drawamount + "Yuan"); Try{thread.sleep (1000);} catch (Exception e) {e.printstacktrace ();} Account.setbalance ((Account.getbalance ()-drawamount)); System.out.println ("Balance remaining:" + (Account.getbalance ()));} ELSE{SYSTEM.OUT.PRINTLN ("Insufficient balance, withdraw money failed!!!");}}}

 

3. Entity class 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;} @Overridepublic int hashcode () {final int prime = 31;int result = 1;result = Prime * result+ ((Accountno = = null)? 0:ac Countno.hashcode ()); Long temp;temp = Double.doubletolongbits (balance); result = Prime * result + (int) (temp ^ (Temp >&G t;>)); return result;} @Overridepublic 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.equal S (other.accountno)) return false;if (Double.doubletolongbits (balance)! = Double.doubletolongbits (other.balance)) return False;return true; 

Second way: Control shared resources

1. Make a fuss in the entity class, make a synchronous method 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;} @Overridepublic int hashcode () {final int prime = 31;int result = 1;result = Prime * result+ ((Accountno = = null)? 0:ac Countno.hashcode ()); Long temp;temp = Double.doubletolongbits (balance); result = Prime * result + (int) (temp ^ (Temp >&G t;>)); return result;} @Overridepublic 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.equal S (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 () + "customer came .... Want to take "+drawamount"), if (Balance-drawamount > 0) {System.out.println (Thread.CurrentThread (). GetName () + "Remove:" + Drawamount); try {thread.sleep (+);} catch (Interruptedexception e) {e.printstacktrace ();} Balance-= Drawamount; System.out.println ("Still left:" + balance);} Else{system.out.println ("Balance unworthy now failed");}}} 2. Withdrawal thread public class Drawthread extends thread {Private account Account;private double drawamount;public drawthread (account A Ccount, String name, double drawamount) {super (name); this.account = Account;this.drawamount = Drawamount;} @Overridepublic void Run () {Account.drawmoney (Drawamount);}} 3. Test public class Drawdemo {public static void main (string[] args) {Account account = new Account ("12347866",);D rawt Hread d = new Drawthread (account, "Zhang San", +);D rawthread d1 = new Drawthread (account, "John Doe", +);D rawthread d2= new Drawthread (account, "Harry", +);d. Start ();d 1.start ();d 2.start (); }

//The Third Way, the implementation of the Runnable interface

1. Test start public class Drawdemo {public static void main (string[] args) {Account account = new Account ("12347866",;D) rawthr EAD d = new Drawthread (account, $);D rawthread D1 = new Drawthread (account, $);D rawthread d2 = new Drawthread (account, 100); thread T1 = new Thread (d, "Monkey King"); Thread t2 = new Thread (d1, "Pig"); thread t3 = new Thread (d2, "small white Dragon"); T1.start (); T2.start (); T3.start ();}} 2. Withdrawal thread public class Drawthread implements Runnable {private account account;private double drawamount;public Drawthread (Acc Ount account, double drawamount) {this.account = Account;this.drawamount = Drawamount;} @Overridepublic void Run () {synchronized (account) {if (Account.getbalance () > Drawamount && Account.getbalance () >= 0) {System.out.println ("sufficient balance," + thread.currentthread (). GetName () + "Remove" + Drawamount + "Yuan"); Try{thread.sleep (1000);} catch (Exception e) {e.printstacktrace ();} Account.setbalance ((Account.getbalance ()-drawamount)); System.out.println ("Balance remaining:" + (Account.getbalance ()));} ElSE{SYSTEM.OUT.PRINTLN ("Insufficient balance, withdraw money failed!!!");}} Account.drawmoney (Drawamount);}} 3. Entity class--shared resource 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;} @Overridepublic int hashcode () {final int prime = 31;int result = 1;result = Prime * result+ ((Accountno = = null)? 0:ac Countno.hashcode ()); Long temp;temp = Double.doubletolongbits (balance); result = Prime * result + (int) (temp ^ (Temp >&G t;>)); return result;} @Overridepublic 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 () + " The customer has come .... Want to take "+drawamount"//if (Balance-drawamount > 0) {//system.out.println (Thread.CurrentThread (). GetName () + "Remove:" + Drawamount),//try {//thread.sleep (+),//} catch (Interruptedexception e) {//e.printstacktrace ();//}//balance-= Drawamount;//system.out.println ("Still left:" + balance);////} else{//system.out.println ("The balance unworthy now fails");//}//}}

Fourth: Synchronizing via Reentranlock

Other places as before package Com.ckang.drawmoney;import Java.util.concurrent.locks.reentrantlock;public class Account { Private final Reentrantlock lock = new Reentrantlock ();p rivate 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;} @Overridepublic int hashcode () {final int prime = 31;int result = 1;result = Prime * result+ ((Accountno = = null)? 0:ac Countno.hashcode ()); Long temp;temp = Double.doubletolongbits (balance); result = Prime * result + (int) (temp ^ (Temp >&G t;>)); return result;} @Overridepublic 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)) retur N 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 () + " The customer has come .... Want to take "+drawamount"), if (Balance-drawamount > 0) {System.out.println (Thread.CurrentThread (). GetName () + "Remove:" + Drawamount); try {thread.sleep (+);} catch (Interruptedexception e) {e.printstacktrace ();} Balance-= Drawamount; System.out.println ("Still left:" + balance);} Else{system.out.println ("The balance unworthy has failed");}} Finally{lock.unlock ();}}}

  

 

 

 

Multithreading Take money

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.