|
The critical section is actually the object monitor
Because multi-threaded operation is out of order
For example: Thread 1 enters the method and runs A = a ;, but has not run return A * A;
The CPU time slice is preempted, another thread enters the operation, and a new value of a is passed in because it is static.
So the change of the A value by the second thread will affect the first thread, causing an error in the return value, and the problem does not occur in the instance
If necessary, synchronize this number, or don't cache A. Unless shared variables, it is best not to use static, synchronization affects performance. There are many synchronization methods, such as lock, mutex, etc. There is only one single variable. |
|