Thread synchronization code block: two customers save money to one bank, and each user saves one hundred at a time for 30 times. Simulate the Bank Deposit function, and the number of cash in the bank at any time ., Save 30 times
Package com. swift; public class Bank_Customer_Test {public static void main (String [] args) {/** two customers save money to one bank, and each user saves one hundred at a time for thirty times. Simulate the Bank Deposit function and the number of cash in the bank at any time. */Customer customer = new Customer (); new Thread (customer, "zhangsan "). start (); new Thread (customer, "lisi "). start () ;}} class Bank {private static int sum = 0; public void setSum (int money) {sum = sum + money; System. out. println (Thread. currentThread (). getName () + "Save money:" + money + "total Bank funds:" + sum) ;}} class Customer implements Runnable {private bank Bank = new bank (); public void run () {for (int I = 0; I <30; I ++) {synchronized ("lock") {bank. setSum (100 );}}}}