Java and contract: Atomicboolean and Atomicreference__java

Source: Internet
Author: User

Article translated from: http://tutorials.jenkov.com/java-util-concurrent/index.html
This series of articles has basically finished, if anything wrong, please criticize.
Please indicate the origin of the transfer. Atomicboolean

Atomicboolean is a boolean-type variable that reads and writes atoms. This contains advanced atomic operations, such as Compareandset (). Atomicboolean is located in the Java.util.concurrent.atomic package, so the full class name is Java.util.concurrent.atomic.AtomicBoolean. The Atomicboolean version, described in this article, can be found in Java 8, with the first version added to Java 5.

The principle behind Atomicboolean design is explained in my other article compare and swap. Create Atomicboolean

You can create a atomicboolean like the following:

Atomicboolean Atomicboolean = new Atomicboolean ();

This example creates a Atomicboolean default value of FALSE.

If you need to display the set Atomicboolean initial value, you can pass an initial value to Atomicboolean.

Atomicboolean Atomicboolean = new Atomicboolean (true);
Gets the value of the Atomicboolean

You can use the Get () method to obtain the Atomicboolean value, and here is an example:

Atomicboolean Atomicboolean = new Atomicboolean (true);

Boolean value = Atomicboolean.get ();

After executing the code above the value variable will be ture. Set the value of Atomicboolean

You can set the Atomicboolean value using the Set () method, which is an example:

Atomicboolean Atomicboolean = new Atomicboolean (true);

Atomicboolean.set (FALSE);

The value of the Atomicboolean variable will be false when the above code is executed. Swap the value of the Atomicboolean.

You can use Getandset () to Exchange Atomicboolean values. The Getandset () method returns the current value of Atomicboolean and sets a new value to it. Here is an example:

Atomicboolean Atomicboolean = new Atomicboolean (true);

Boolean oldValue = Atomicboolean.getandset (false);

After executing the code above oldvalue the value of the True,atomicboolean instance is false. The code effectively exchanged the Atomicboolean value with the current value of true to false. Compare and set values for Atomicboolean

Compareandset () allows you to compare the current value of Atomicboolean with the value you expect. If the current value is the value you expect, a new value is set on the Atomicboolean. The Compareandset () method is atomic, so only one thread is allowed to execute it at the same time. Thus, the Compareandset () method can be used to implement a simple synchronizer such as a lock.

Here is an example of using Compareandset ():

Atomicboolean Atomicboolean = new Atomicboolean (true);

Boolean expectedvalue = true;
Boolean newvalue      = false;

Boolean wasnewvalueset = Atomicboolean.compareandset (
    expectedvalue, newvalue);

In this example, the current value of Atomicboolean is compared to true, and if two values are equal, a new value of Atomicboolean will be set to false. atomicreference

The Atomicreference class provides an object reference variable that reads and writes Atomically. Atoms mean that multiple threads trying to change the same atomicreference (for example, compare and swap operations) will not cause atomicreference to be in an inconsistent state. Atomicreferenc's Compareandset () method allows it to be compared to a desired value, and if they are equal, the objects in Atomicreference are set to a new reference. Create Atomicreference

You can create an instance of Atomicreference below.

Atomicreference atomicreference = new Atomicreference ();

If you need to use an initial reference to create a atomicreference, you can do so as follows.

String initialreference = "The initially referenced string";
Atomicreference atomicreference = new Atomicreference (initialreference);
Create generic Atomicreference

You can use Java generics to create a typed atomicreference. Here is an example:

atomicreference<string> atomicstringreference =
    new atomicreference<string> ();

You can also specify an initial value for typed Atomicreference, and here is an example of specifying an initial value for a typed atomicreference:

String initialreference = "The initially referenced string";
atomicreference<string> atomicstringreference =
    new atomicreference<string> (initialReference);
Get atomicreference Reference

You can use the Atomicreference get () method to obtain a reference stored in atomicreference. If you use the Get () method on a Non-generic atomicreference, an object reference will be returned. Using the Get () method on a generic atomicreference will return a reference to the type you declared on the atomicreference.

The following is an example of a non-generic atomicreference get ():

Atomicreference atomicreference = new Atomicreference ("A-referenced");

String reference = (string) atomicreference.get ();

Note that when Atomicreference is a non-generic, the reference returned by the Get () method must be cast to string because get () is an object reference.

The following is an example of a generic atomicreference:

atomicreference<string> atomicreference = 
     new Atomicreference<string> ("A-referenced");

String reference = Atomicreference.get ();

Note that there is no need to cast the reference returned by get () because the compiler knows it returns a string reference. Set Atomicreference Reference

You can use the set () method to set the reference stored in the Atomicreferenc instance. For instances of Non-generic Atomicreference, the Set () method takes an object reference as an argument. The Atomicreference,set () method for generics will use the reference of the type you declared on Atmoicreference as an argument.

The following is an example of a atomicreference set ():

Atomicreference atomicreference = 
     new Atomicreference ();

Atomicreference.set ("New object referenced");

References to generics and non-generic do not look different when using set (). The only difference is that the compiler has a type constraint on the atomicreference of the generic type. Compare and set atomicreference references

Atomicreference has a very useful method of Compareandset (). The Compareandset () method can make a comparison of the references stored in atomicreference with your expected value if they are the same (not equal as in equals () but same as in = =), Then a new reference is set on the Atomicreference instance.

If the Compareandset () method sets a new reference to Atomicreference, it returns true. Otherwise, false is returned.

The following is an example of the Atomicreference Compareandset () method:

String initialreference = "initial value referenced";

atomicreference<string> atomicstringreference =
    new atomicreference<string> (initialReference);

String newreference = "new value referenced";
Boolean exchanged = Atomicstringreference.compareandset (initialreference, newreference);
System.out.println ("exchanged:" + exchanged);

exchanged = Atomicstringreference.compareandset (initialreference, newreference);
System.out.println ("exchanged:" + exchanged);

The above example uses an initial reference to create a generic atomicreference. The two Compareandset () method is then invoked to compare the stored reference and the initial reference. If the stored reference is equal to the initial reference, a new reference will be set. The first time two references are equal, so atomicreference is set up with a new reference. A second stored reference is a reference to the new setting that was previously called Compareandset (), so the stored reference must not be equal to the initial reference. As a result, atomicreference is not set to a new reference, and the Compareandset () method returns FALSE.

Related Article

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.