PackageCom.swift; Public classBank_customer_test { Public Static voidMain (string[] args) {/** Two customers deposit money to a bank, save 30 times each, save 100. Simulate bank saving function, the bank cash number at all times. */Customer Customer=NewCustomer (); NewThread (Customer, "Zhangsan")). Start (); NewThread (Customer, "Lisi")). Start (); }}classBank {Private Static intsum = 0; Public voidSetsum (intMoney ) {Sum= Sum +Money ; System.out.println (Thread.CurrentThread (). GetName ()+ "Save money:" +money+ "Bank total Funds:" +sum); }}classCustomerImplementsRunnable {PrivateBank Bank =NewBank (); Public voidrun () { for(inti = 0; I < 30; i++) { synchronized("Lock") {bank.setsum (100); } } }}
Thread synchronization code block: Two customers deposit money to a bank, save 30 times each, save 100. Simulate bank saving function, the bank cash number at all times.