Java – how to crack this synchronization code?
•
Java
I have the following code snippet
synchronized (mObject) { if (mObject.variable != -1) { doThis(); doThisAsWell(); andThis(); insertObjectInDb(); // This is crashing because mObject.variable is -1 } }
As far as I know, if I use synchronized on an object and then run the block, no other thread can change the value of the variable, right? I don't understand that the value of the variable is - 1. When I go to the next block, I check it specially No, all functions in the block do not change the value Have I completely misunderstood how these blocks work?
If it is important, then this is the doinbackground () method for asynchronous tasks in Android applications
Any ideas?
Solution
No, your assumption is wrong Synchronized is a monitor Your only guarantee is to access the monitor one thread at a time
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码