How to use Java Thread to simulate money withdrawal
This article mainly introduces the relevant knowledge of "how to use Java thread simulation to withdraw money". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to use Java thread simulation to withdraw money" can help you solve the problem.
Public class SynchronizedTTest {public static void main (String [] args) {Account account=new Account (100, "gifts"); Drawing you=new Drawing (account,80, "miserable"); Drawing wife=new Drawing (account,90, "happy"); you.start (); wife.start ();}} class Account {int money;String name;public Account (int money, String name) {this.money = money;this.name = name;}} class Drawing extends Thread {Account account / / the amount of money in the account (intended to pass into the Account object) the number of money withdrawn by int drawingMoney;// the name of the incoming thread public Drawing (Account account, int drawingMoney,String name) {super (name); this.account = account;this.drawingMoney = drawingMoney;} @ Override public void run () {account.money-=drawingMoney;packetMoney+=drawingMoney;System.out.println (this.getName () + "- > balance is" + account.money) System.out.println (this.getName () + "- > money in pocket is" + packetMoney);}} this is the end of the introduction to "how to use Java thread simulation to withdraw money". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.