Home  >  Article  >  Java  >  About Java thread synchronization

About Java thread synchronization

巴扎黑
巴扎黑Original
2017-04-15 09:06:071849browse

1、synchronized method.

public synchronized void save(){}

2、synchronized code block.

synchronized(object){ 
}

3、Use volatile variables.
Using volatile to modify the field is equivalent to telling the virtual machine that the field may be updated by other threads, so each time the field is used, it must be recalculated instead of using the value in the register.

4、Use reentrant lock (ReenreantLock)

public void save(int money) {
    lock.lock();
    try {
        account += money;
    } finally {
        lock.unlock();
    }
}

5、Use ThreadLocal variable, then each thread using the variable will obtain a copy of the variable, and the copies will be independent of each other, so Each thread can modify its own copy of the variable at will without affecting other threads.

6、Use blocking queues (LinkedBlockingQueue, etc.)

7、Use atomic variables (AtomicInteger, etc.)

Summary:
synchronized:
In situations where synchronization occurs occasionally, synchronized is very suitable. The reason is that compilers usually optimize synchronize as much as possible. Synchronized is implemented at the JVM level. Not only can synchronized locks be monitored through some monitoring tools, but also if an exception occurs during code execution, the JVM will automatically release the lock.

ReentrantLock:
ReentrantLock provides diverse synchronization, such as time-limited synchronization, synchronization that can be interrupted (synchronized synchronization cannot be interrupted), etc. When resource competition is not fierce, the performance is slightly worse than synchronized. But when synchronization is very intense, the performance of synchronized can suddenly drop dozens of times. And ReentrantLock can indeed maintain normalcy. Lock is implemented through code. To ensure that the lock will be released, unlock() must be placed in finally{}.

Atomic:
The performance is slightly worse than synchronized when it is not intense, but it can maintain normalcy when it is intense. In intense situations, Atomic's performance will be about twice as good as ReentrantLock's.


1、synchronized method.

public synchronized void save(){}

2、synchronized code block.

synchronized(object){ 
}

3、Use volatile variables.
Using volatile to modify the field is equivalent to telling the virtual machine that the field may be updated by other threads, so each time the field is used, it must be recalculated instead of using the value in the register.

4、Use reentrant lock (ReenreantLock)

public void save(int money) {
    lock.lock();
    try {
        account += money;
    } finally {
        lock.unlock();
    }
}

5、Use ThreadLocal variable, then each thread using the variable will obtain a copy of the variable, and the copies will be independent of each other, so Each thread can modify its own copy of the variable at will without affecting other threads.

6、Use blocking queues (LinkedBlockingQueue, etc.)

7、Use atomic variables (AtomicInteger, etc.)

Summary:
synchronized:
In situations where synchronization occurs occasionally, synchronized is very suitable. The reason is that compilers usually optimize synchronize as much as possible. Synchronized is implemented at the JVM level. Not only can synchronized locks be monitored through some monitoring tools, but also if an exception occurs during code execution, the JVM will automatically release the lock.

ReentrantLock:
ReentrantLock provides diverse synchronization, such as time-limited synchronization, synchronization that can be interrupted (synchronized synchronization cannot be interrupted), etc. When resource competition is not fierce, the performance is slightly worse than synchronized. But when synchronization is very intense, the performance of synchronized can suddenly drop dozens of times. And ReentrantLock can indeed maintain normalcy. Lock is implemented through code. To ensure that the lock will be released, unlock() must be placed in finally{}.

Atomic:
The performance is slightly worse than synchronized when it is not intense, but it can maintain normalcy when it is intense. In intense situations, Atomic's performance will be about twice as good as ReentrantLock's.


The above is the detailed content of About Java thread synchronization. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn