Multi-threaded programming--the types and discrimination of PART5 locks

Source: Internet
Author: User
Tags semaphore

A lock in Java can be divided into a synchronous lock and a lock in a JUC package.

Sync Lock

Through the Synchronized keyword synchronization, to achieve mutually exclusive access to competing resources of the lock,.

Principle: For each object, there is only one synchronization lock, at the same point in time, all the threads have and only one can get the synchronization lock, get to the synchronization lock can be scheduled by the CPU, other threads must wait until the synchronization lock to continue to run, This is how multithreading is synchronized through synchronous locks.

Juc lock in Package: Java.util.concurrent

  More powerful, and of course more complex to use.

Juc package lock, including: Lock interface, Readwritelock interface, Locksupport blocking primitives, condition conditions abstractownablesynchronizer/ Abstractqueuedsynchronizer (AQS)/abstractqueuedlongsynchronizer three abstract classes, Reentrantlock exclusive locks, Reentrantreadwritelock read and write locks. Countdownlatch,cyclicbarrier and semaphore are also implemented through AQS; A frame diagram of a lock

1.LOCK interface

The lock interface in the JUC package supports locking rules that differ in semantics (re-entry, fairness, and so on). The so-called semantic differences, refers to the lock is a "fair mechanism of the lock", "non-fair mechanism of the lock", "reentrant lock" and so on. "Fairness mechanism" refers to "the mechanism of acquiring locks by different threads is fair", while "unfair mechanism" means "the mechanism of acquiring locks by different threads is unfair", "reentrant lock" means that the same lock can be acquired multiple times by a thread.

2.ReadWriteLock

The Readwritelock interface defines a lock in a manner similar to lock that some readers can share while the writer is exclusive. JUC package only one class implements the interface, that is, Reentrantreadwritelock, because it applies to most of the standard usage contexts. But programmers can create their own implementations that apply to non-standard requirements.

3.Abstractownablesynchronizer/abstractqueuedsynchronizer/abstractqueuedlongsynchronizer

Abstractqueuedsynchronizer is called the AQS class, which is a very useful superclass that can be used to define locks and other synchronizers that rely on queued blocking threads; Reentrantlock, These classes, such as Reentrantreadwritelock,countdownlatch,cyclicbarrier and semaphore, are implemented based on the Aqs class. The Abstractqueuedlongsynchronizer class provides the same functionality but extends support for 64 bits of the synchronization state. Both extend the class Abstractownablesynchronizer, a simple class that helps to record threads that are currently holding exclusive synchronizations.

4.LockSupport

Locksupport provides "create locks" and "basic thread blocking primitives for other synchronization Classes".
Locksupport's functionality is somewhat similar to the Thread.Suspend () and Thread.Resume () in Thread, where Park () and Unpark () in Locksupport are blocking threads and unblocking threads, respectively. However, Park () and Unpark () do not encounter the "deadlock that may be caused by thread.suspend and thread.resume" issues.

5.Condition

Condition needs to be used in conjunction with lock, which acts as a substitute for the object monitor method, and can hibernate/wake the thread through await (), signal ().
The Condition interface describes the condition variables that may be associated with a lock. These variables are similar in usage to implicit monitors that use object.wait access, but provide more powerful functionality. It should be noted that a single Lock may be associated with multiple Condition objects. To avoid compatibility issues, the name of the Condition method differs from the corresponding Object version.

6.ReetrantLock

Reentrantlock is an exclusive lock. The so-called exclusive lock, refers to only be occupied alone, that is, the same point in time can only be acquired by a thread lock. Reentrantlock locks include "fair reentrantlock" and "non-fair reentrantlock". "Fair reentrantlock" means that "the mechanism by which different threads acquire locks is fair", while "unfair reentrantlock" means "the mechanism by which different threads acquire locks is unfair" and reentrantlock are "reentrant locks".

(1) The Reentrantlock implements the lock interface.
(2) Reentrantlock has a member variable sync,sync is the sync type; sync is an abstract class, and it inherits from Aqs.
(3) Reentrantlock "Fair lock class" Fairsync and "unfair lock class" nonfairsync, all of which are subclasses of sync. The Sync object in Reentrantreadwritelock is one of the Fairsync and Nonfairsync, which means that Reentrantlock is one of the "fair locks" or "unfair locks", Reentrantlock default is a non-fair lock.

7.ReetrantReadWriteLock

Reentrantreadwritelock is the implementation class for the read-write lock interface Readwritelock, which includes subclasses Readlock and Writelock. Readlock is a shared lock, while Writelock is an exclusive lock.

(1) Reentrantreadwritelock implements the Readwritelock interface.
(2) Reentrantreadwritelock contains sync object, read lock ReaderLock and write lock WriterLock. Both the read lock Readlock and the write lock Writelock implement the lock interface.
(3) Like "Reentrantlock", Sync is the sync type, and sync is an abstract class that inherits from Aqs. Sync also includes "fair lock" Fairsync and "unfair lock" nonfairsync.

8.CountDownLatch

Countdownlatch is a synchronous helper class that allows one or more threads to wait until a set of operations that are performed in another thread is completed. The countdownlatch contains the Sync object, and sync is the sync type. Countdownlatch's sync is the instance class, which inherits from Aqs.

9.CyclicBarrier

Cyclicbarrier is a synchronous helper class that allows a group of threads to wait for each other until a common barrier point (common barrier points) is reached. Because the barrier can be reused after releasing the waiting thread, it is called a cyclic barrier.

Cyclicbarrier is the "Reentrantlock object lock" and "condition Object Trip", which is implemented by an exclusive lock.
the difference between Cyclicbarrier and Countdownlatch is:
(1) The role of Countdownlatch is to allow 1 or n threads to wait for other threads to complete execution, while Cyclicbarrier allows n threads to wait for each other.
(2) Countdownlatch counter cannot be reset, Cyclicbarrier counter can be reset and used, so it is called loop barrier.

10.Semaphore

Semaphore is a count semaphore, and its essence is a "shared lock".
The semaphore maintains a semaphore license set. A thread can obtain a semaphore's license by calling acquire (), and the thread can obtain the license when there is a license available in the semaphore, otherwise the thread must wait until a license is available. The thread can release () The semaphore license it holds.

The semaphore contains the Sync object, Sync is the sync type, and sync is an abstract class that inherits from Aqs. Sync also includes "fair semaphore" Fairsync and "non-fair semaphore" Nonfairsync.

Multi-threaded programming--the types and discrimination of PART5 locks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.