The fifth chapter java concurrent learning - lock thread

A fair locking and non-locking fair

  Thread the so-called fair, meaning that the threads are in accordance with the order to apply the lock to get the lock, if it is to comply with the order to get, this is a fair locks, otherwise non-fair locks.

  Lock unfair advantage is that high throughput, but since it is not to follow the order to lock the application to acquire the lock, the lock will be the beginning of the application has not been acquired, leading to starvation phenomenon.

  java in use:

  synchronized: unfair Lock

  ReentrantLock: a method configured according to specify whether the lock latch is fair, fair non-default lock

Second, the share lock and exclusive lock

  Exclusive lock: The lock can only be one thread to use;

  Shared locks: the lock can be used by a plurality of threads;

  java in use:

  synchronized: exclusive lock (which is inevitable)

  ReentrantLock: exclusive lock 

  ReadWriteLock: ReadWriteLock is divided into two read-write locks, which read lock is a shared lock, write lock is an exclusive lock, this advantage is such that the lock of high resource utilization, because just reading the words will not change the content.

Third, tend to lock, lock lightweight, heavyweight lock

  These three are only locked state, and only for these three locks in synchronized

  Biased locking: When the other thread competition, will release the lock. This is because the presence of lock acquire and release locks are a huge waste of resources, if only one thread, biased locking is a very good choice.

  Lightweight lock: when the multithreaded atomicity operation, allowing multiple threads simultaneously enter the synchronized code block. This lock is a lock to become biased when multiple threads from a single-threaded evolution.

  Heavyweight lock: When the lock is a lightweight lock, another thread though is the spin, but spin will not last forever, when the spin a certain number of times, has not yet acquired lock, will enter the obstruction, the lock inflation heavyweight lock. Heavyweight thread lock to make other applications into the blocking performance.

  Before JDK5, synchronized has been a heavyweight lock.

Fourth, the re-entry lock

  Reentrant lock means in a multithreaded environment, if there are two threads A, B, and A, B two threads acquire the same lock, if only at this time A thread calls Thread B, A will automatically get B in the lock. Reentrant lock benefit is the ability to avoid deadlock.

  ReentrantLock are synchronized with the re-entry lock.

Fifth, the spin lock

  In Java, the spin lock refers to the attempt to acquire the lock thread does not block immediately, instead of using a circular fashion to try to acquire the lock, this benefit is to reduce the consumption of thread context switching, the drawback is that the cycle will consume CPU.

Sixth, deadlock, livelock, starvation

  These three are state of the thread:

  Deadlock: When a thread gets the lock, other threads can also be obtained deadlock occurs

  Livelock: When two threads simultaneously acquire the same resources, if you have found each other in obtaining the resources, then went to visit the two sides will stop simultaneously access the same resource to another, and so forth to produce a live lock.

  Hunger: a lower priority thread can not execute

Seven, optimistic and pessimistic locking lock

  Pessimistic locks: always assume the worst case, get a time when data modification think others will, so every time she took the data will be locked, so people want to take this data will be blocked until the lock to get it .

  Optimistic locking: As the name suggests, is very optimistic, pick up data every time when they are that others will not be modified, so it will not be locked, but when the update will determine what others during this time did not go to update the data, you can use mechanism version number.

  java in use:

  Pessimistic lock: synchronized

  Optimistic locking: JDK classes atomic operations java.util.concurrent.atomic

  

  

  

Guess you like

Origin www.cnblogs.com/daijiting/p/11574195.html