How does JVM make sure threads acquire a lock after entering synchronized method of an object?
问题:
回答1:
Broad question:
How does the JVM make sure...?
The "VM" in "JVM" stands for "virtual machine." Your code doesn't do anything by itself. When we say, "your code runs", what we really mean is, the JVM executes your instructions. And it does so in accordance with the rules that are laid out in the JVM specification. One of the rules says that a JVM must never execute a synchronized block for two different threads on the same object at the same time.
But there are many layers to the onion: A typeical JVM uses native threads (i.e., threads provided by the operating system) to implement Java threads, and it typically relies on mutex objects provided by the OS to synchronize the threads.
Going deeper still, neither the JVM nor the operating system really does anything by itself: It's the computer hardware executing the instructions of the OS and the JVM that really makes things happen.
The complete answer to "how does synchronization work?" is a couple of chapters from a book about operating system design, plus a couple of chapters from a book on computer architecture, plus a shot of computer science on the side. To fully understand it all, you'll at least need to know about:
- "user-mode instructions" vs. "priviliged mode instructions",
- How system calls work,
- How an operating system "scheduler" performs "Context Switches"
- hardware synchronization primitives like "Compare and Swap (CAS)", "Test and Set (TAS)", "load-link/store-conditional (LL/SC)"
They're all subjects that you can look up on Wikipedia, but IMO, books are better for learning a subject of that depth.
回答2:
To coordinate shared data access among multiple threads, the Java virtual machine associates a lock with each object and class. A lock is like a privilege that only one thread can "possess" at any one time. If a thread wants to lock a particular object or class, it asks the JVM. At some point after the thread asks the JVM for a lock -- maybe very soon, maybe later, possibly never -- the JVM gives the lock to the thread. When the thread no longer needs the lock, it returns it to the JVM. If another thread has requested the same lock, the JVM passes the lock to that thread.
Check out the entire article for more information : http://www.javaworld.com/article/2076971/java-concurrency/how-the-java-virtual-machine-performs-thread-synchronization.html
回答3:
There are whole books written on the subject of low level implementations and concurrent features.
But for those looking for basic understanding of how it's all wired up and what's the logic behind it: JVM implements "synchronised" feature using intrinsic lock provided by Java Built-in Monitor Objects. On the lower level Java built-in monitor object synchronizers can be implemented w/POSIX-like synchronizers, that's system level C libraries.
I highly recommend going through these two youtube courses which explain the basics in very nice way so you can quickly get a good grasp of it and learn its semantics:
Java Built-in Monitor Objects: Overview and Motivating Example
https://www.youtube.com/watch?v=vHAWxXCB9Bg&list=PLZ9NgFYEMxp4UHEwQCltQciArqXDyn6Ms&index=49
Java Built-in Monitor Objects: Coordination
https://www.youtube.com/watch?v=QNi1BH9JcJE&list=PLZ9NgFYEMxp4UHEwQCltQciArqXDyn6Ms&index=51