Concurrent programming-Concurrency data types

Source: Internet
Author: User

In the parallel environment, the synchronous (lock) operation is the biggest enemy to achieve true parallelism, and a lot of lock competition consumes the system resources seriously , but in the development, we can not avoid multiple threads to modify the shared variables, and JDK1.5 provides us with a set of classes to implement the lock-free algorithm (CAS algorithm). They packed the array types for Integer,long,boolean and Integer,long respectively.


Atomicinteger (Long) Core method:


Public final int Get () Get Current value
Public final void set (int newvalue) Set Current value
Public final int Getandset (int newvalue) Set a new value and return the old value
Public final Boolean compareamdset (int expect,int u) If the current value is expect, set to U
Public final int getandincrement () Current value plus 1, return old value
Public final int getanddecrement () The current value is reduced by one, returning the old value
Public final int getandadd (int delta) Current value increment delta, return old value
Public final int Incrementandget () Current value plus 1, return new value
Public final int Decrementandget () Current value minus 1, return new value
Public final int addandget (int delta) Add Delta to current value, return new value




refer to the JDK Help documentation for other classes or methods

Atomicreference
Atomicreferencearray
Atomicboolean
Atomicinteger
Atomicintegerarray
Atomiclong
Atomiclongarray




Let's take a look at how CAS algorithm manifests itself in Atomicinteger

Java code
  1. /**
  2. * Atomically sets to the given value and returns the old value.
  3. *
  4. * @param newvalue
  5. * The new value
  6. * @return the previous value
  7. */
  8. public Final int getandset (int newvalue) {
  9. for (;;) { //The dead loop keeps trying until it succeeds
  10. int current = get (); //Get current value
  11. //If the current value is not affected by another thread, set to the new value, otherwise enter the second loop
  12. if (Compareandset (current, newvalue))
  13. return current;
  14. }
  15. }



in the CAS algorithm, first the program body is in an infinite loop, after obtaining the current value, using Compareandset to compare, if the other thread to modify the values, Compareandset will return false to enter the second loop until the success at the hardware level, most modern processors already support the atomized CAS directive, which can be used by the JVM to implement concurrent operations and concurrency data structures after JDK5.0.


In addition to the atomic operation classes of Integet and long, the JDK provides a more exciting type atomicreference<v> he can reference all reference types

Java code
  1. Public class Testmain {
  2. @Test
  3. public void Testatomicreference () throws Exception {
  4. /* 
  5. Note
  6. * Classes to be referenced using Atomicreference are best to override the Equals method
  7. * Compareandset will use = = To determine if the object is equal
  8. */
  9. Raytest test1 = new Raytest ();
  10. Raytest test2 = new Raytest ();
  11. //Initialize atomicreference, default value is Test1
  12. atomicreference<raytest> reference = new atomicreference<raytest> (test1);
  13. //Replace the test1 with Test2
  14. Reference.compareandset (Test1, test2);
  15. Assert.assertnotsame (Test1, Reference.get ());
  16. Assert.assertequals (Test2, Reference.get ());
  17. }
  18. }



in the CAS algorithm, the whole process does not need to lock, no wait, no lock operation can actually be multiple threads of concurrency conflict processing by the application level, thereby improving the performance of the system, but the use of CAS algorithm coding complexity significantly increased, Fortunately the JDK has provided us with a atomic class that allows us to use it directly, recommending a similar need for direct use of the provided tools

Concurrent programming-Concurrency data types

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.