Java Concurrent Programming Series Concurrent Package overview

Source: Internet
Author: User

Starting with JDK 1.5, the Java.util.concurrent package was added, and the introduction of the concurrent package greatly simplifies the development of multi-threaded threads.

Looking at the JDK API, you can see that the Java.util.concurrent package is divided into three parts,
They are java.util.concurrent, java.util.concurrent.atomic and Java.util.concurrent.lock,respectively.

>>atomic Bag

The instructions in the API are "A small toolkit of classes that support Lock-free Thread-safe programming on single variables".
The atomic package facilitates the programmer to perform atomic operations without locking in a multithreaded environment.
The bottom of the atomic variable uses the atomic instructions provided by the processor, but different CPU architectures may provide an atomic directive that may require some form of internal lock, so the method does not absolutely guarantee that the thread will not be blocked.
The atomic variable class is equivalent to a generalized volatile variable that supports both atomic and conditional read-write operations.
In the atomic package, there are 12 classes, four atomic update methods, namely atomic Update primitives, Atomic update arrays, atomic update references, and atomic update fields.

The classes in the atomic package are basically wrapper classes that are implemented using unsafe.

(1) Scalar class (scalar): atomicboolean,atomicinteger,atomiclong,atomicreference
Atomicboolean,atomicinteger,atomiclong,atomicreference these four basic types to handle Boolean, Integer, Long Integer, object four kinds of data, its internal implementation is not a simple use of synchronized, It is a more efficient way of CAS (Compareandswap) +volatile and native methods, which avoids the high overhead of synchronized and greatly improves execution efficiency.
Their implementations are real values of the volatile type, implemented through atomic operations in the unsafe package. The most basic is the CAS, he is the foundation of everything. Where offset is the offset of value in memory relative to the base address. (Its acquisition is also obtained by the unsafe local code).
The core code is as follows, others are built on compareandset basis.

Privatestaticfinalunsafeunsafe=unsafe.getunsafe ();p rivatevolatileintvalue;publicfinalintget () {returnvalue;} Publicfinalvoidset (intnewvalue) {value=newvalue;} Publicfinalbooleancompareandset (intexpect,intupdate) {Returnunsafe.compareandswapint (This,valueOffset,expect, Update);}

 

(2) Array class: Atomicintegerarray,atomiclongarray,atomicreferencearray
Atomicintegerarray,atomiclongarray also has a Atomicreferencearray class that further expands atomic operations and provides support for these types of arrays.
The same is done with the unsafe class.

(3) Updater class: Atomiclongfieldupdater,atomicintegerfieldupdater,atomicreferencefieldupdater
A reflection-based utility that can atomically update a specified volatile field of a specified class.

(4) Compound variable class: Atomicmarkablereference,atomicstampedreference

>> multithreaded Infrastructure Packages

The classes and interfaces below the Java.util.concurrent package define the methods that JDK1.5 can use to write multi-threaded procedures.
(1) Common interface methods
Blockingqueue and Blockingdeque blocking queues and double-ended blocking queues
Executor: Performer of specific runnable tasks
Executorservice: A thread pool manager with a variety of implementation classes that can submit runnable,callable to the pool for scheduling
Scheduledexecutorservice: A executorservice sub-interface that can schedule commands that run after a given delay or are executed on a regular basis
Future:future represents the result of an asynchronous calculation

(2) Implementation class overview
Countdownlatch: After a thread calls the await method, it blocks to wait for the counter to be called Countdown until it becomes 0, functionally and below the cyclicbarrier a bit like
Cyclicbarrier: A synchronization helper class that allows a group of threads to wait for each other until a common barrier point is reached
Semaphore: a count semaphore]

>>lock and its related applications

Located under the Java.util.concurrent.locks package, mainly related to the new concurrent lock.

(1) Interface method
Condition interface: The lock implementation provides a wider range of locking operations than is possible with synchronized methods and statements. There are two implementation classes that are implemented in the form of public internal classes, Abstractqueuedlongsynchronizer.conditionobject, Abstractqueuedsynchronizer.conditionobject
Lock interface: There are three implementation classes, Reentrantlock, Reentrantreadwritelock.readlock, Reentrantreadwritelock.writelock
Readwritelock interface: Readwritelock maintains a pair of related locks, one for read-only operations and the other for write operations. As long as there is no writer, read locks can be persisted by multiple reader threads at the same time. The write lock is exclusive.
Read-write locks allow a higher level of concurrent access to shared data than a mutex. Although only one thread at a time (writer thread) can modify shared data, in many cases, any number of threads can concurrently read shared data (reader threads), read-write locks take advantage of this. The implementation class has only one, Reentrantreadwritelock.
(2) Implementation class overview
Reentrantlock: A re-entrant lock lock.
Reentrantreadwritelock: A read-write lock that can be re-entered.
There are also a few, so learn to be good abstract.

Refer to the Atomic Package usage Guide in Java

Java Concurrent Programming Series Concurrent Package overview

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.