Multi-threaded Classic issues window selling tickets

Source: Internet
Author: User

Requirements: Simple ticket sales procedures, to achieve multiple windows at the same time buy tickets.
One, mode one:

1. Question: Why 50 tickets have been sold 150 times.

2, the reason for the occurrence: Because num is non-static, non-static member variable data in each object will maintain a copy of the data, three thread objects will have three.

3, the solution: the number of num votes shared out to three thread objects to use. Use static adornments.

Two, mode two:

Note: Static member variables must be defined outside the member method and not in the member method.
1. Problem: Thread safety problem has occurred.

the root cause of thread-safety problems:
1 There are two or more than two thread objects, and a resource is shared between threads.
2 There are multiple statements operating the shared resource. (Thread safety issues are not present if only a single statement operates on a shared resource)

2. Solution to Thread-safety problems: Sun provides a thread synchronization mechanism for us to solve such problems.

The thread synchronization mechanism is detailed in the next article.

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.