Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example of thread safety in Java bank withdrawal

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains the "Java bank withdrawal thread safety example explanation", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's train of thought slowly in-depth, together to study and learn "Java bank withdrawal thread safety example explanation"!

This paper gives an example to describe the thread safety problem of Java bank withdrawal. Share with you for your reference, the details are as follows:

One defines an account class

Public class Account {/ / encapsulates the account number and two member variables of the account balance: private String accountNo; private double balance; public Account () {} / / Constructor public Account (String accountNo, double balance) {this.accountNo = accountNo; this.balance = balance;} / / the setter of accountNo and balance and the setter and getter methods of getter / / accountNo are omitted here. Public void setAccountNo (String accountNo) {this.accountNo = accountNo;} public String getAccountNo () {return this.accountNo } / / balance's setter and getter methods public void setBalance (double balance) {this.balance = balance;} public double getBalance () {return this.balance;} / / the following two methods rewrite hashCode () and equals () methods public int hashCode () {return accountNo.hashCode ();} public boolean equals (Object obj) {if (this = = obj) return true; if (obj! = null & & obj.getClass () = Account.class) {Account target = (Account) obj Return target.getAccountNo () .equals (accountNo);} return false;}}

Second, define a money withdrawal thread

Public class DrawThread extends Thread {/ / simulate user account private Account account; / / the amount of money that the current withdrawal thread wants to withdraw private double drawAmount; public DrawThread (String name, Account account, double drawAmount) {super (name); this.account = account; this.drawAmount = drawAmount;} / / when multiple threads modify the same shared data, data security issues will be involved. Public void run () {/ / account balance is greater than the number of withdrawals if (account.getBalance () > = drawAmount) {/ / spit out banknotes System.out.println (getName ()) + "money withdrew successfully! Spit out banknotes: "+ drawAmount); try {Thread.sleep (1);} catch (InterruptedException ex) {ex.printStackTrace ();} / / modify the balance account.setBalance (account.getBalance ()-drawAmount); System.out.println ("\ t balance: "+ account.getBalance ());} else {System.out.println (getName () +" failed to withdraw money! Insufficient balance! ") ;}

Three test main classes

Public class DrawTest {public static void main (String [] args) {/ / create an account Account acct = new Account ("1234567", 1000); / / simulate two threads to withdraw money from the same account new DrawThread ("A", acct, 1000). Start (); new DrawThread ("B", acct, 800). Start ();}}

Four operation

B succeeded in withdrawing money! Spit out banknotes: 800.0 A withdraws money successfully! Spit out banknotes: 800.0 balance: 200.0 balance:-600.0

Five instructions

When the account balance is only 1000, 1600 is withdrawn, and the account balance is negative, which is what the bank wants to see.

Although the above program artificially uses Thread.sleep (1) to force a thread scheduling switch, such a switch is entirely possible.

Thank you for your reading, the above is the "Java bank withdrawal thread safety example to explain" the content, after the study of this article, I believe you have a deeper understanding of the Java bank withdrawal thread safety example to explain this problem, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report