Public class testseven extends thread {
Private Static int X;
Public synchronized void dothings (){
Int current = X;
Current ++;
X = current;
}
Public void run (){
DoThings ();
}
}
Which statement is true?
A. Compilation fails.
B. An exception is thrown at runtime.
C. Synchronizing the run () method wocould make the class thread-safe.
D. The data in variable "x" are protected from concurrent access problems.
E. Declaring the doThings () method as static wocould make the class thread-safe.
F. Wrapping the statements within doThings () in a synchronized (new Object () {} block wocould make
Class
Thread-safe.
Answer: E
Why is it d? Is the answer wrong?
------------------------------------
Because dothings is a non-static function, synchronized locks the current object. If multiple testseven objects exist, the methods of different objects lock different objects separately, and the synchronization problem will occur. The core of synchronization is to lock the same object. Therefore, the answer is e instead of D.
PS: To achieve thread-safe in the code, either change X to non-static or change dothings () to static.
------------------------------------------
Because x is static, it is shared by all TestSeven objects. Therefore, when multiple thread objects TestSeven execute the doThings method, although this method is synchronized, the lock object is this, therefore, the doThings method of a thread object changes x, but the doThings method can be executed for another thread object to get its own lock mark, but x is changed, there is a security problem.
Changing the method to static can ensure thread security.
The common synchronization method locks the monitor associated with this, while the static synchronization method is the monitor associated with the Class object corresponding to the Class containing this method;
Therefore, the lock objects of each static method are the same, so there is no such insecure factor.
I don't know if my expression is cleared...
----------------------------------------------
I only partially agree with you. Looking at this code, x is modified in each independent thread. Even if multiple TestSevent instances run at the same time, what is the impact? X is first assigned to the local variable current, calculation occurs in current, and then assigned to x, so I think there is no thread security problem, and the assignment is an atomic operation.
----------------------------------------
X is static. Imagine that two TestSeventd objects t1 and t2 share an x
T1.start (), t2.start ()
T1 thread execution
Java code
Public synchronized void doThings () {int current = x; // 1 Assume that x = 1 current ++; // 2 x = current; // 3}
After doThings is called this time, x = 2
However, when the thread has not executed 1, the time segment of the thread is used up, and the cup is handed over to the other thread T2.
T2 also executes dothings () once, changes X to 2, and then returns to the T1 thread. T1's dothings continues to be executed. After the execution, x = 3 instead of 2, an error occurred.
Because it is two testseventd, there is no mutual exclusion between the two threads (that is, the lock does not work), and X is static, so this is not safe.