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.