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

Source: Internet
Author: User

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 );}}}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.