As a Java programmer, not knowing concurrent programming is clearly not enough to satisfy the market, especially if you are in a passive position during the interview process, which may or may not be over.
So as a Java developer, although you can implement concurrent programming based on Java concurrent toolkit, but do you really understand the principle and mechanism behind it? Put yourself to the test: Have you ever used the word synchronized? What are the underlying principles?Java Concurrent Synchronization
Synchronized is a mutually exclusive access mode implemented by the JVM, which is based on a per-object monitor. Synchronized modified code, after compiled by the compiler, prefixes the modified code with a set of byte instructions.
Monitorenter is added at the beginning of the code and Monitorexit at the end. These bytecode instructions work together to achieve mutually exclusive access to the synchronized keyword modification code.
When a virtual machine executes a Monitorenter command, it requests a monitor lock on an object, which is used to derive a lock counter.
Java concurrency locks
When monitorenter is executed, if the object is not locked or the current thread already owns the monitor lock for the object, the lock counter +1 is acquired by the thread.
When Monitorexit is executed, the lock counter -1, and when the counter is 0, the object lock is released. Other blocking threads can then request the Monitor lock.
Given the above principles, let’s consider a question: What can synchronized modify? What’s the difference when decorating static and non-static methods? Welcome to leave comments and discuss