In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Today, I would like to share with you how java multithreading to achieve Mini Program withdrawals related knowledge, detailed content, clear logic, I believe that most people are too aware of this knowledge, so share this article for your reference, I hope you have something to gain after reading this article, let's take a look at it.
1. Establish the java class
Here, three java classes are established, the first Account class is used to encapsulate all kinds of information in the account, the third DrawThread class is used to implement the thread body, and the second class mainly encapsulates the main function
2. The AccountClass
Public class Account {/ / encapsulates account number, account balance and two member variables private String accountNo; private double balance; public Account () {}; public Account (String accountNo,double balance) {this.accountNo=accountNo; this.balance=balance;} public void setAccountNo (String accountNo) {this.accountNo=accountNo;} public void setBalance (double balance) {this.balance=balance } public String getAccountNo () {return accountNo;} public double getBalance () {return balance;} public int hashCode () {return accountNo.hashCode ();} public boolean equals (Object obj) {if (this==obj) {return true;} if (obj.getClass () = Account.class) {Account target= (Account) Class Return target.getAccountNo () .equals (accountNo);} return false;}}
3. DrawThread class
Public class DrawThread extends Thread {/ / simulate user account private Account account; / / the number of withdrawals desired by the current withdrawal thread 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 () {/ / use account as the synchronization monitor, any thread must obtain a lock on the account account before entering the following synchronization code block-other threads cannot acquire the lock Also cannot modify it synchronized (account) {if (account.getBalance () > = drawAmount) {/ / spit out banknotes System.out.println (getName ()) + "withdraw money 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! ") ;}
4. DrawTest class
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 ("jack", acct,800). Start (); new DrawThread ("rose", acct,800). Start ();}}
Running result:
One thing to note about the above program is that it uses synchronous code blocks. It can solve the problem that the run () method does not have synchronization security (that is, it will cause an exception when two threads send a request at the same time)
Synchronize code blocks:
Synchronized (obj) {/ / Code to be executed}
If we remove the synchronization block from the run () method and continue to run
Account acct=new Account ("1234567", 1000); new DrawThread ("jack", acct,800). Start (); new DrawThread ("rose", acct,800). Start ()
(the total amount of the bank account is 1000 yuan. Jack and rose withdraw money from the same account respectively)
Running result:
These are all the contents of the article "how to withdraw Mini Program with java multithreading". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.