Home  >  Article  >  Java  >  Example analysis of interview questions about locking in Java

Example analysis of interview questions about locking in Java

WBOY
WBOYforward
2023-05-05 08:13:061457browse

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 object Memory is locked (note that only this memory, other similar objects will have their own memory locks), at this time, executing the synchronization method of the object in more than one other thread (note: it is the object) will generate mutual exclusion

Static method: equivalent to locking the class (*.class is located in the code area, 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 it is an object generated by this class, mutual exclusion will occur when calling this static method. 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) No lock&Lightweight lock&Bias lock&Heavyweight lock

(5)Interaction Exclusion lock & shared lock

(6) Fair lock&unfair lock

3. Several ways of thread synchronization

synchronized modification

volatile achieves synchronization (only visibility is guaranteed, not atomicity)

Use local variables ThreadLocal

Use atomic classes (AtomicInteger, AtomicBoolean...)

Use Lock

Use container classes (BlockingQueue, ConcurrentHashMap)

4. The difference between synchronized and lock mechanisms

synchronized originally used CPU pessimistic locking mechanism, that is, the thread obtains 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 implemented by optimistic locking is CAS operation (Compare and Swap).

5. Talk about the issue of thread safety

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 multiple threads. environment without problems.

In Java multi-threaded programming, multiple ways to achieve Java thread safety are provided:

The simplest way is to use the Synchronization keyword

Use java.util. Atomic classes in the concurrent.atomic package, such as AtomicInteger

Use locks in the java.util.concurrent.locks package

Use thread-safe collections ConcurrentHashMap

Use volatile keys words to ensure variable visibility

The above is the detailed content of Example analysis of interview questions about locking in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete