Java geek

Related reading:

Java Concurrent programming (1) Knowledge map Java concurrent programming (2) Atomic Java concurrent programming (3) Visibility Java concurrent programming (4) Sequential Java concurrent programming (5) Introduction to Creating threads Java concurrent programming (6) Synchronized usage Introduction to Concurrent Programming in Java (7) Easy to understand wait and Notify and application scenarios Introduction to Concurrent programming in Java (8) Thread lifecycle Introduction to Concurrent programming in Java (9) Deadlock and deadlock bit Introduction to Concurrent programming in Java (11) Flow limiting scenarios and Spring Flow limiter implementation Introduction to Concurrent Programming in Java (12) Producer and Consumer pattern – Introduction to concurrent programming in Java (13) Read/write lock and cache template (14) CountDownLatch Application Scenario Java Concurrent programming (15) CyclicBarrier application scenario Introduction to Concurrent programming in Java (16) seconds to understand the difference between the thread pool Java Concurrent programming introduction (17) one picture master common classes and interfaces for threads Java concurrent programming introduction (18) Again on thread safety Java concurrent programming introduction (19) Asynchronous task scheduling tool CompleteFeature Java Concurrent programming introduction (20) Common locking scenarios and locking tools


First, lock optimization

Lock optimization is as follows: 1. Use lock-free solution 2. Avoid deadlocks 3. Lock separation 5. Lock coarsening 6. Reduce lock granularity

Deadlock-free schemes are introduced separately to avoid deadlocks for reference: Introduction to Concurrent Programming in Java (9) deadlock and deadlock bits.

Two, reduce lock holding time

Synchronize only the blocks of code that need to be synchronized, not the entire method, such as a second check of the singleton pattern.

public class Singleton {

    private static Singleton singleton;

    private Singleton(a) {}

    public static Singleton getInstance(a) {
        if (null == singleton) {
            // Reduce the lock holding time instead of putting the synchronized keyword on methods
            synchronized (Singleton.class) {
                if (null == singleton) {
                    singleton = newSingleton(); }}}returnsingleton; }}Copy the code

Three, lock separation

1. Separate read/write locks for data that is read or written too much. Read/write locks are not mutually exclusive, but read/write locks are mutually exclusive, which improves reading efficiency. 2. For linked list data structures, if fetching data is front-end fetching and adding data is back-end fetching, separate fetching and storing locks, refer to LinkedBlockingQueue.

Four, lock coarsening

Lock acquisition operations consume resources. If an operation requires a lock to be acquired frequently and subsequent operations are completed quickly, lock acquisition operations can be combined. For example, if the same lock is acquired in a loop statement, the lock acquisition operation can be placed outside the loop.

for (int i = 0; i < size; i++) {
    synchronized(lock) {
        //do something}}Copy the code

Five, reduce the lock granularity

For example, a map is used to cache data. Each key is a type of data, such as id type and customer type. In this way, the lock can be separated from each key when the map is operated, and the key can be locked when the data of a certain type is operated. The ConcurrentHashMap class in Java is internally divided into 16 segments, and each segment has its own lock. In this way, up to 16 concurrent operations can be performed, which also improves efficiency by reducing the lock granularity.

end.


<– Read the mark, left like!