When we talk about thread safety, we will choose the lock method to solve it. The types of locks in Java are divided into six types. The two most common lock methods we have come across are lock and synchronized. This is also the most frequently asked principle and mechanism question in interviews. Below we will show you the common interview questions in Java. Let’s see if you can answer them all.
1.The difference between synchronized effects on static methods and non-static methods
Non-static methods:
Lock the object (can be understood as locking the memory of this object. Note that it is only this memory, other similar objects will have their own memory locks). At this time, the synchronization method of the object is executed in more than one other thread (note : is the object) will generate mutual exclusion
Static method : equivalent to locking the class (*.class is located in the code area, and the static method is located in the static area. The objects generated by this class share this static method, so N objects compete for this memory),
At this time , as long as the object generated by this class is called, mutual exclusion will occur when this static method is called. That is, all objects of this class share a lock.
2.What are the lock types?
(1) Optimistic lock & pessimistic lock
(2) Spin lock & non-spin lock
(3) Reentrant lock & non-reentrant lock
(4) Lockless & lightweight lock & biased lock & heavyweight lock
(5) Mutex lock & shared lock
(6) Fair lock & unfair lock
3. Several methods of thread synchronization
synchronized modification
volatile implements synchronization (only visibility is guaranteed, not atomicity)
Use local variable ThreadLocal
Use atomic classes ( AtomicInteger, AtomicBoolean...)
Use Lock
Use the container class ( BlockingQueue, ConcurrentHashMap)
4. The difference between synchronized and lock mechanisms
synchronized originally used the CPU pessimistic locking mechanism, that is, the thread obtained an exclusive lock. An exclusive lock means that other threads can only rely on blocking to wait for the thread to release the lock.
Lock uses optimistic locking. The so-called optimistic locking is to complete an operation without locking each time but assuming that there is no conflict. If it fails due to a conflict, it will be retried until it succeeds. The mechanism for implementing optimistic locking is CAS operation (Compare and Swap).
5. Talk about thread safety issues
Thread safety is an issue in the field of multi-threading. Thread safety can be simply understood as a method or an instance that can be used in a multi-threaded environment without problems.
In Java multi-threaded programming, multiple ways to achieve Java thread safety are provided:
The simplest way, use the Synchronization keyword
Use atomic classes from the java.util.concurrent.atomic package, such as AtomicInteger
Using locks from the java.util.concurrent.locks package
Use thread-safe collection ConcurrentHashMap
Use the volatile keyword to ensure variable visibility
The above is an introduction to the interview questions about locks in Java. For knowledge points that you are not clear about, you must find information in time to solve them and strengthen your memory of the lock content.