An interesting phenomenon about Java multi-threading

Source: Internet
Author: User

Simulate a ticketing system, or a bank to withdraw money.

Class Bank {

Synchronized Getmoney () {

Here to sleep, in order to slow down the speed.

}

}

Class Customer extends thread{

Withdraw Money () {

Loop 100 times {

Getmoney ()

Interestingly, hibernation is also required here.

}

}

}

Class test{

Main () {

Customer 1.start ()

Customer 2.start ()

}

}

Such a topic, in the past only when the money, there is the need for sleep, did not expect to write when the discovery. The place of the cycle also sleeps.

Otherwise it will appear, customer 1 after the completion of the customer 2 only start the situation.

Feel that the cycle will be a one-time 100 of the task of the money to send out, although each time the money is relatively slow (to sleep), but the task is distributed quickly.

Not after the money, go back to the loop, and then randomly decide the customer n execution.

An interesting phenomenon about Java multi-threading

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.