preface
In interviews with Java programmers, the basics section is often asked about the difference between Lock and Synchronized. In fact, this is just an entry point, if the interviewer is interested in it, he will start from here and examine the whole system of concurrent programming knowledge.
Let’s start with the Java Concurrent programming knowledge network.
answer
A good answer to the following question goes something like this:
Synchronized is a Java keyword. At the VIRTUAL machine level, object monitors implement atomic LOCK and unlock operations on VMS, requiring a specific lock.
Lock is an interface under the Java Concurrent package. Using it to do the lock operation can mainly realize the controllable interrupt, can realize the fair lock, can bind multiple conditions. Before java1.6, lock was more efficient than syn. However, after lock optimization in 1.6 and later, the efficiency is almost the same. Lock optimization includes: spin and adaptive, lock elimination, lock slang, lightweight lock, biased lock.
So after 1.6, the reason to choose Lock is to achieve more functions, not efficiency.
The principle of synchronized keyword should be understood from the virtual machine level. The next issue may be the difference between Synchronized methods and Synchronized blocks of code; Lock upgrade process and so on.
Lock keyword, need to know how the Java language level is implemented, here will involve the implementation of lock, AQS, CAS and other concepts, and then ask if there is any relevant practice, Classic issues like CountDownLatch, the use of Cyclicrrier locks, thread pool usage and principles, etc.
Knowledge network
Now let’s go back and review the theoretical framework of concurrent programming that you need to master as a Java developer. Here we’re looking at it from the bottom up.
Basic knowledge of
Beginner Java will contact multi-threaded programming, which is also a major feature and advantage of the Java language at that time, of course, now look at the granularity of the thread concurrency some can not keep up. Common issues include the difference between processes and threads, how threads are created, thread lifecycle and state transitions, thread synchronization and thread communication, and so on. There are also some classic practices: producer consumer problems, banker algorithms, multi-threaded circular printing, and so on.
JMM memory model theory and CPU atomic instructions
Related to Synchronized are the JMM memory model theory, the concepts of working memory and main memory, visibility, reordering, and atomicity issues, the concepts of memory barriers, and so on
Related to Lock are CPU caches, CAS instructions, and so on.
Lock related applications, Synchronized and other Java keywords
AQS (AbstractQuenedSynchronizer abstract queue type synchronizer) related content, the unsafe, locksupport, some commonly used concurrent programming tools (CountDownLatch, CyclicBarrier, Sepmopore…) ; Synchronized lock upgrade process, other concerns (volatile, final) and principle and application.
The thread pool
Common thread pool types, implementation principles, Fork/Join framework, CompletableFuture, etc. A common problem is how to set the thread pool size/parameters.
Here is a picture taken from the Internet, basically the whole knowledge system can be in the shape of ‘#’.
summary
This is the framework for Concurrent Programming in Java, which will be covered later. In addition, the thread granularity concurrency mentioned at the beginning is not enough to support some of the current application scenarios of the Internet. The concept of “coroutines” and “pipe procedures” will be introduced, and will be shared later.