Java threads and bags for Util.Concurrent (3)

xiaoxiao2021-03-06  42

The most basic classes in the util.concurrent package of Mr. Dawid Kurzyniec in Dawid Kurzyniec are atomic classes in edu.mory.mathcs.Backport.java.util.Concurrent.atomic. Here I first start from Atomicinteger class LOOK. .

Atomicinteger class description:

A integer value can be updated by atomically. An Atomicinteger is used as an atomic growth counter to be used in the application, and Java.lang.integer is used in the application. But this class is an extension of Number abstract classes, which can be processed by NUMBER-based tool access. The following is the source code of Atomicinteger:

// Atomicinteger class extends Number class and can be serialized

Public class atomicinteger extends Number imports java.io.serializable {

PRIVATE STATIC FINAL Long SerialVersionuid = 6214790243416807050L;

// integer value

Private int value;

/ **

* Construct an Atomicinteger with an initialization value.

*

* @PARAM initialization value

* /

Public atomicinteger (int initialvalue) {

Value = InitialValue;

}

/ **

* Use 0 to construct an Atomicinteger..

* /

Public atomicinteger () {

}

/ **

* Get the value of Atomicinteger.

*

* @return Returns the current value of Atomicinteger

* /

Public factory synchronized int get () {

Return Value;

}

/ **

* Assign a new value to Atomicinteger

*

* @Param incoming new value

* /

Public Final Synchronized Void Set (INT NewValue) {

Value = newValue;

}

/ **

* Set the new value and return the old value.

*

* @Param incoming new value

* @return returns the old value

* /

Public final synchronized int getandset (int newvalue) {

INT OLD = VALUE;

Value = newValue;

Return OLD;

}

/ **

* Atomically update specified value for new value

* If the current value is equal to the expected value (Expect)

* @Param expectation value

* @Param incoming new value

* @Return If the update is successful, return TRUE. Otherwise returning false

* /

Public Final Synchronized Boolean Compareand (Int Expect, Int Update) {

Boolean Success = (Expect == Value);

IF (SUCCESS)

Value = Update;

RETURN SUCCESS;

}

/ **

* Atomically update specified value for new value

* If the current value is equal to the expected value (Expect).

* Maybe it may fail.

* @Param expectation value

* @Param incoming new value

* @Return If the update is successful, return TRUE.

* /

Public Final Synchronized Boolean WeakCompareAnd (int expect, int update) {

Boolean Success = (Expect == Value); if (Success)

Value = Update;

RETURN SUCCESS;

}

/ **

* Athant increments 1

* @return returns the old value before adding

* /

Public final synchronized int getandincrement () {

Return Value ;

}

/ **

* Athant increments 1

* @Return returns new value after the increase

* /

Public final synchronized int incrementandget () {

Return Value;

}

/ **

* Atharomad reduced 1

* @Return returns the old value before the decrement

* /

Public final synchronized int getandDecrement () {

Return Value--;

}

/ **

* Atharomad reduced 1

* @return returns new value after decreasing

* /

Public factory synchronized int distrib rangendget () {

Return - Value;

}

/ **

* Athant increases an increment (DELTA)

* @Param increment DELTA

* @return returns the old value before adding

* /

Public final synchronized int getandd (int delta) {

INT OLD = VALUE;

Value = Delta;

Return OLD;

}

/ **

* Athant increases an increment (DELTA)

* @Param increment DELTA

* @Return returns new value after the increase

* /

Public final synchronized int addandget (int delta) {

Return Value = DELTA;

}

/ **

* Atomically updated the old value for the new value

*

* @Param incoming new value

* @return returns the old value

* /

Public final synchronized int getandset (int newvalue) {

INT OLD = VALUE;

Value = newValue;

Return OLD;

}

/ **

* Returns a string expression of the previous value.

* @return returns a string expression of the previous value.

* /

Public string toString () {

Return integer.tostring (get ());

}

Public int INTVALUE () {

Return get ();

}

PUBLIC Long longvalue () {

RETURN (long) get ();

}

PUBLIC FLOAT floatvalue () {

Return (Float) Get ();

}

Public Double DoubleValue () {

Return (Double) Get ();

}

}

Overall, the basic working principle is to use synchronous synchronized methods to achieve increase, minus, assignment (update) for a integer value. Make the value of Atomicinteger into atomic operations.

In addition, Atomiclong, AtomiclongArray, Atomicintegerarray, AtomicBoolean, AtomicReference, AtomicMarkableReference, AtomicMarkableReference, AtomicReferenceArray, the working principle of the AtomicsTampedReference class is the same.

转载请注明原文地址:https://www.9cbs.com/read-68679.html

New Post(0)