Ext: 52057289
Article translated from: http://tutorials.jenkov.com/java-util-concurrent/index.html
This series of articles has been basically finished, if anything wrong, please criticize correct.
Please indicate the source of the transfer.
Atomicboolean
Atomicboolean is a Boolean type of variable that reads and writes are atomic. This includes 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 version of Atomicboolean described in this article can be found in Java 8, and the first version is added in Java 5.
The principle behind Atomicboolean design is explained in my other article compare and swap.
Create Atomicboolean
You can create a atomicboolean as follows:
AtomicBoolean atomicBoolean = new AtomicBoolean();
This example creates a Atomicboolean default value of FALSE.
If you need to display the initial value of the setting Atomicboolean, you can pass an initial value to Atomicboolean.
new AtomicBoolean(true);
- Gets the value of the Atomicboolean
You can get the value of Atomicboolean using the Get () method, here is an example:
new AtomicBoolean(true);boolean value = atomicBoolean.get();
After executing the above code, the value of the values variable will be ture.
- Set the value of the Atomicboolean
You can set the value of the Atomicboolean using the Set () method, which is an example:
new AtomicBoolean(true);atomicBoolean.set(false);
The value of the Atomicboolean variable will be false after executing the above code.
- The value of the interchange Atomicboolean.
You can use Getandset () to Exchange values for Atomicboolean. The Getandset () method returns the current value of Atomicboolean and sets a new value for it. Here is an example:
new AtomicBoolean(true);boolean oldValue = atomicBoolean.getAndSet(false);
After executing the above code, the value of OldValue is false for the value of the True,atomicboolean instance. The code effectively swaps the Atomicboolean value to true for the current value so that it is false.
- Compare and set the value of 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 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's an example of using Compareandset ():
new AtomicBoolean(true);boolean expectedValue = true;boolean newValue = false;boolean wasNewValueSet = atomicBoolean.compareAndSet( expectedValue, newValue);
This example compares the current value of Atomicboolean with true, and if two values are equal, a new value of Atomicboolean is set to false.
Atomicreference
The Atomicreference class provides an object reference variable that reads and writes are atomic. 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 compare to a desired value, and if they are equal, the object in the Atomicreference is set to a new reference.
Create Atomicreference
You can create an instance of Atomicreference as follows.
AtomicReference atomicReference = new AtomicReference();
If you need to use an initial reference to create a atomicreference, you can do just as follows.
String initialReference = "the initially referenced string";AtomicReference atomicReference = new AtomicReference(initialReference);
- Create a 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 assign an initial value to a 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 References
You can use the Atomicreference get () method to obtain a reference stored in the 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.
Here is an example of the get () of a non-generic atomicreference:
new AtomicReference("first value 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 put in an object reference.
Here is an example of a generic atomicreference:
AtomicReference<String> atomicReference = new AtomicReference<String>("first value 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.
Setting Atomicreference References
You can use the set () method to set a reference stored in an Atomicreferenc instance. For instances of non-generic atomicreference, the Set () method takes an object reference as a parameter. The Atomicreference,set () method for generics will use a reference to the type you declared on Atmoicreference as a parameter.
The following is an example of a atomicreference set ():
AtomicReference atomicReference = new AtomicReference();atomicReference.set("New object referenced");
References to generics and non-generic types are not different on 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 is Compareandset (). The Compareandset () method can make a comparison of the references stored in the atomicreference with your expected values if they are the same (not equal as in equals () and same as in = =), A new reference is then 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); "new value referenced"; out.println ( "exchanged:" + exchanged); exchanged = Atomicstringreference.compareandset (Initialreference, newreference); System. out.println ( "exchanged:" + exchanged);
In the example above, an initial reference was used to create a generic atomicreference. Then call two times the Compareandset () method to compare the stored reference with 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. The second stored reference is a reference to the new setting that was previously called Compareandset (), so the stored reference must be not equal to the initial reference. Therefore, Atomicreference is not set to a new reference, and the Compareandset () method returns FALSE.
(GO) Java and contract: Atomicboolean and Atomicreference