Java thread safety basics

Copyright: original works, posted please indicate the source! https://blog.csdn.net/sa19861211/article/details/90313525

1. synchronized

JMM about two synchronized provisions:

  1. Before thread unlock the value of shared variables must be the latest refresh to the main memory
  2. Thread lock, will clear the working memory shared values of variables, requiring from the main memory when using shared variables
    to re-obtain the latest value (Note: locking and unlocking needs are the same lock)
    by more than two points, you can see It can be synchronized to achieve visibility. Meanwhile, since the synchronization lock synchronized with, so it is atomic.
    Conclusion: synchronized not only solves the visibility, but also solve the atomicity

2. Lock

2.1 ReentrantLock reentrant lock
in the preamble code needs to be locked, but do not forget at the end of the lock must be released, or will never be able to cause the lock is released, causing the other thread never enter.
ReentrantReadWriteLock 2.2
ReentrantReadWriteLock its core is a separate read and write lock at high concurrent access, especially in the case of reading and writing less, the performance is far higher than reentrant lock.
Its essence is divided into two locks, read and write locks. In a read lock multiple threads can concurrently access, but when write lock, only one by one sequential access.
Rules: Read sharing, reading and writing are mutually exclusive, write mutually exclusive.

3. volatile

the role of the volatile keyword is to force the thread to the main memory (shared memory) to go read a variable, rather than into the working memory thread in reading. Never realized the variables between multiple threads visible, that is, to meet the visibility thread-safe.
Conclusion: volatile only address visibility, does not solve the atomic.

4. Atomic Atomic class

For example: AtomicInteger, AtomicLong ...
support atomic operations, but only to ensure that the method of atom itself, does not guarantee atomicity multiple operations.

5. ThreadLocal

Thread local variables, it is a multi-threaded solution between concurrent access to variables.
Unlike its synchronized manner such as locking, ThreadLocal completely locks are not provided, but the use of instruments to space for time, provide a copy of the independent variables for each thread to ensure thread safety.
From the performance said, ThreadLocal does not have an absolute advantage. In the concurrent it is not very high, locking the performance will be better, but as a set of locks completely unrelated thread-safe solutions in high-volume concurrent or competitive scene, using ThreadLocal can be reduced to some extent lock competition.

6. Lock optimization

Lock optimization should follow the following principles:

  • Make use of non-lock operation, for example: atomic operations (Atomic * series type), and the volatile keyword.
  • Reducing the holding time locks
  • Reduced lock granularity
  • Separating lock (write lock)
  • Avoid deadlock

Guess you like

Origin blog.csdn.net/sa19861211/article/details/90313525