cool hit counter Atomic Operations Class for Concurrent Programming_Intefrankly

Atomic Operations Class for Concurrent Programming

The array value is passed into the AtomicIntegerArray by construction, and the AtomicIntegerArray actually makes a copy of the current array.

source (of information etc):

Introduction to atomic manipulation classes

The atomic operation class is equivalent to the generalizedvolatile variable, Capable of supporting atomic reads- modify- write operation。 for exampleAtomicInteger indicates aint Value of type, providesget harmonyset approach, suchvolatile Variables of type have the same memory semantics for reads and writes。 There are a total of atomic operation classes13 category, (located) atjava.util.concurrent.atomic contract (a company), There are four types of atomic update classes that can be classified: Atomic update basic type、 Atomic update array types、 Atomic update references and atomic update properties。

Each of these four atomic operation classes will be described below.

Atomic update basic type

Update the basic type using an atomic approach, consisting of 3 classes.

Atomicboolean: Atomic update of boolean variables

AtomicInteger: Atomic update of integer variables

AtomicLong: Atomic update long integer variables

The methods provided in the source code for each class specifically are essentially the same, and are illustrated here using AtomicInteger as an example. Some of the methods provided by AtomicInteger are as follows.

To illustrate the atomic nature of AtomicInteger, here is code demonstrating a multi-threaded self-increment operation on an int value, with the final output, as follows.

The output is as follows.

You can see that in the multi-threaded case, the result is correct, but if you just use a member variable of type int you may get a different result. The key here is that getAndIncrement is an atomic operation, so how is that guaranteed?

The source code for the getAndIncrement method is as follows.

By this point you can see that the native method is finally called to ensure atomicity of the update.

Atomic update array

pass (a bill or inspection) Atomic update array An element in the, in all3 category:

AtomicIntegerArray: Atomic update of an element of an integer array

AtomicLongArray: Atomic update of an element of a long integer array

AtomicReferenceArray: Atomic update of an element of an array of reference types

Commonly used methods of AtomicIntegerArray are.

int addAndSet(int i, int delta): adds the input value to the element in the array with index i in an atomic way

boolean compareAndSet(int i, int expect, int update): if the current value is equal to the expected value, update the value in the array with index i to the update value in an atomic way

The sample code is as follows.

The result of the run is :

The array value is passed into AtomicIntegerArray by construction, and AtomicIntegerArray actually makes a copy of the current array, so the operation in the array copy does not affect the value of the original array.

Atomic update reference type

The need to update reference types often involves multiple variables, and the earlyatomic package has three classes.

AtomicReference: Atomic update reference type

AtomicReferenceFieldUpdater: Atomic update of fields in reference types

AtomicMarkableReference: atomic update of reference types with markable bits.

The following is an example of AtomicReference.

You can see that the user was successfully updated.

Atomic Update Field Class

If you need to update a field of a class atomically, you need to use the Atomic Update Field class, which can use the following classes.

AtomicIntegerFieldUpdater: Atomic update of integer fields

AtomicLongFieldUpdater: Atomic update of long integer fields

AtomicStampedReference: atomic update of reference types with version numbers.

To update a field atomically, two steps are required.

An updater must be created each time using newUpdater and the fields of the class you want to update need to be set

The fields (properties) of the update class must be public volatile

The following code demonstrates how to update a field using the Atomic Update Field class.

The output is as follows.

At this point, we know how to use the basic usage of the atomic operation class in different scenarios.

1、HDFS Knowledge Summary
2、Video Conferencing System PolycomHDX Remote Command Execution Vulnerability Analysis
3、Winform set up comboboxtext can not be displayed problem
4、US realtime AI platform EdgyBees raises 55 million in seed funding
5、Some useful settings for SublimeText3

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送