Java Atomic Reference Atomicreference

Source: Internet
Author: User

Link: https://www.jianshu.com/p/882d0e2c3ea6: Jane book full-time Atomicreference

An object reference the May updated atomically.

AtomicReferenceThe class provides reference objects. May is read and written atomically, so if multiple threads try to reach them at the same time, only one would be able to doing so.
Provides a read-write atomic operation of a reference variable.

The following methods are provided:

    • compareAndSet(V expect, V update): atomically Sets the value to the given updated value if the current value == the expected value .
    • getAndSet(V newValue): atomically sets to the given value and returns the old value.
    • lazySet(V newValue): Eventually sets to the given value.
    • set(V newValue): Sets to the given value.
    • get(): Gets the current value.

Suppose there is a class person, which is defined as follows:

Classperson {private String name;Privateint age;PublicPerson(String name,int age) {THIS.name = name;This.age = age; }Public StringGetName() {return name; }PublicvoidSetName(String name) {this.name = name;} public int getage () {return age;} public void setAge  (int age) {this.age = age;} public String tostring () {return " [Name: "+ this.name +  "Age:" + this.age + Span class= "hljs-string" > "]"; }} 

If you use normal object references, updating objects in multithreaded situations can cause inconsistencies. For example:
The initial state of an object is name=Tom, age = 18 .
Thread 1 will name be modified to Tom1 , age + 1 .
Thread 2 will name be modified to Tom2 , age + 2 .

We think that there will only be two kinds of results:

    • If thread 1 executes first, thread 2 executes, the middle state is name=Tom1, age = 19 , and the result state is name=Tom2, age = 21
    • If thread 2 executes first, thread 1 executes, the middle state is name=Tom2, age = 20 , and the result state is name=Tom1, age = 21

However, the possible output is as follows:

Person is [Name:tom, age:18]
Thread2 Values [Name:tom1, age:21]
Thread1 Values [Name:tom1, age:21]
Now person is [name:tom1, age:21]

General referencesPrivatestatic person person;PublicStaticvoidMain(string[] args)Throws Interruptedexception {person =New Person ("Tom,"18); System.out.println ("Person is" + person.tostring ()); Thread T1 =New Thread (New Task1 ()); Thread t2 =New Thread (New Task2 ()); T1.start (); T2.start (); T1.join (); T2.join (); System.out.println ("Now person is" + person.tostring ());}StaticClassTask1ImplementsRunnable {Publicvoid run () {Person.setAge ( Person.getage () + 1); Person.setname ( "Tom1"); System.out.println ( "Thread1 Values" + person.tostring ());}} static class task2 implements Runnable {public void run () {Person.setage (Person.getage () + 2); Person.setname ( "Tom2"); System.out.println ( "Thread2 Values" + person.tostring ())}}     

If you use an Atomic object reference, updating an object in a multithreaded situation ensures consistency. For example:

General referencesPrivatestatic person person;Atomic ReferencesPrivateStatic atomicreference<person> Arperson;PublicStaticvoidMain(string[] args)Throws Interruptedexception {person =New Person ("Tom,"18); Arperson =New atomicreference<person> (person); System.out.println ("Atomic person is" + arperson.get (). toString ()); Thread T1 =New Thread (New Task1 ()); Thread t2 =New Thread (New Task2 ()); T1.start (); T2.start (); T1.join (); T2.join (); System.out.println ("Now Atomic person is" + arperson.get (). toString ());}StaticClassTask1ImplementsRunnable {PublicvoidRun() {Arperson.getandset (new person ( "Tom1", Arperson.get (). Getage () + 1)"); System.out.println ( "Thread1 Atomic References" + arperson.get (). toString ());}} static class task2 implements Runnable {public void run () {Arperson.getandset (new Person ( "Tom2", Arperson.get (). Getage () + 2)); System.out.println ( "Thread2 Atomic References" + arperson.get (). toString ());}}    

However, the possible output is as follows:

Atomic person is [Name:tom, age:18]
Thread1 Atomic References [Name:tom1, age:19]
Thread2 Atomic References [Name:tom2, age:21]
Now Atomic person is [Name:tom2, age:21]

Reference:
Java atomicreference Example

(RPM) Java Atomic Reference atomicreference

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.