Java Multithreading: "Juc lock" 01 of the framework

Source: Internet
Author: User
Tags thread

Locks in Java can be divided into " synchronous locks " and "locks in a Juc package ", depending on the time the lock was added to Java.

Sync Lock

That is, through the Synchronized keyword to synchronize, to achieve mutually exclusive access to competitive resources of the lock. Sync locks are already supported in the Java 1.0 version.

The principle of synchronous locking is that for each object, there is only one synchronization lock, and different threads can access the synchronization lock together. However, at the same point in time, the synchronization lock can and can only be acquired by one thread. In this way, the thread that gets the sync lock can perform CPU scheduling to execute on the CPU, and the thread that does not get the sync lock must wait until the synchronization lock is acquired before it can continue to run. This is the principle of multithreading synchronization by synchronous lock!

For more information about sync locks, refer to the contents of the "basic part of the Java lock".

Lock in Juc package

The lock in the JUC package is more powerful than a synchronous lock, which provides a framework for locking, which allows more flexibility in the use of locks, but it is more difficult.

Locks in the Juc package, including: Lock interface, Readwritelock interface, Locksupport blocking primitives, condition conditions, abstractownablesynchronizer/ Abstractqueuedsynchronizer/abstractqueuedlongsynchronizer three abstract classes, Reentrantlock exclusive locks, Reentrantreadwritelock read and write locks. Since Countdownlatch,cyclicbarrier and semaphore are also implemented through AQS, I also generalize them to the lock framework for introduction.

First look at the lock frame diagram, as shown below.

Lock interface

The lock interface in the JUC package supports lock rules that are semantically different (reentrant, fair, and so on). The so-called semantic difference, refers to the lock but there is a "fair mechanism of the lock", "unfair mechanism of the lock", "reentrant lock" and so on. "Fair mechanism" means "the mechanism of acquiring locks by different threads is fair", while "unfair mechanism" means "the mechanism of acquiring locks by different threads is unfair" and "reentrant lock" means that the same lock can be acquired by a thread multiple times.

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.