Java concurrency Programming the 15th Chapter atom variable and non-blocking synchronization mechanism

Source: Internet
Author: User
Tags cas volatile

Non-blocking synchronization mechanism

To put it simply, it's about synchronizing and not using locks.

The implementation of a nonblocking algorithm is much more cumbersome than a lock-based scheme, but it has a huge advantage in scalability and activity.

A common way to implement non-blocking algorithms is to use volatile semantics and atomic variables.

Hardware-to-concurrency support

The generation of atomic variables is primarily supported by the processor, and most importantly the CAS (compare and exchange) directives that are supported by most processor architectures.

Analog implementation of Atomicinteger + + operations

First we simulate the CAS syntax of the processor, and the reason is that the CAs are directly supported by atomic operations in the processor. No lock required.

    1. public synchronized int Compareandswap (int exceptvalue, int newvalue) {
    2. int oldValue = count;
    3. if (OldValue = = Exceptvalue) {
    4. Count = newvalue;
    5. }
    6. return oldValue;
    7. }

Note: CAs always returns oldValue.

Use the above method to simulate the Atomicinteger + + operation.

  1. Class myatomicinteger{
  2. private int value;
  3. public int Incrementandget ()
  4. {
  5. int v;
  6. do{
  7. v = value;
  8. }while (v! = Compareandswap (v,v+1));
  9. return v + 1;
  10. }
  11. }

Note: Java's Atomicinteger Implementation mechanism is like this, does not block, uses the processor's CAS function, but polls the attempt.

It seems that polling attempts are worse, but the CAS-based algorithm is better when the competition is not very high.

Atomic class

Atomicboolean Atomicinteger Atomiclong atomicreference

Atomic Array class: Atomicintegerarray Atomiclong atomicreferencearray.

Arrays decorated with volatile syntax can only guarantee the volatile semantics of the array variables themselves, and cannot guarantee the volatile semantics of the elements. The atomic array class should be used at this time.

Note: Atomicboolean Atomicinteger Atomiclong and non-atomic corresponding numeric classes such as Integer are completely unused. The implementation mechanism is completely different and there is no corresponding relationship. One of the most important differences: This three-atom class is mutable. and is the hashcode and Equals method of the object used, which does not extend itself.

Performance comparisons: Locks and Atomic variables

In the medium to low level competition, the atom variable can provide the very high scalability, the Atom variable performance surpasses the lock, but under the intense competition, the lock can avoid the competition more effectively, the lock performance will surpass the atom variable performance. But in a more realistic situation (generally less powerful competition), the performance of the atomic variable will exceed the performance of the lock.

Note: Regardless of whether it is a lock or an atomic variable, it is far less efficient than avoiding shared states (such as using thread-blocking techniques, but using scenarios to limit the usage) to eliminate competition.

Two non-blocking algorithm examples

  1. /**
  2. * Non-blocking stacks constructed using the Treiber algorithm
  3. */
  4. public class Concurrentstack<e> {
  5. private atomicreference<node<e>> top = new atomicreference<concurrentstack.node<e>> ();
  6. Public void Push (E item) {
  7. node<e> newhead = new node<e> (item);
  8. Node<e> Oldhead;
  9. do{
  10. Oldhead = top. get ();
  11. Newhead.next = Oldhead;
  12. } while (!top.compareandset (Oldhead, Newhead));
  13. }
  14. Public E Pop () {
  15. Node<e> Oldhead;
  16. Node<e> Newhead;
  17. Do {
  18. Oldhead = top. get ();
  19. if (Oldhead = = null)
  20. return null;
  21. Newhead = Oldhead.next;
  22. } while (!top.compareandset (Oldhead, Newhead));
  23. return oldhead.item;
  24. }
  25. private static Class node<e>{
  26. Public final E item;
  27. Public node<e> Next;
  28. Public Node (E item) {
  29. This.item = Item;
  30. }
  31. }
  32. }

Below this did not understand, stay here record, later look:

  1. /**
  2. * Insert sort in non-blocking algorithm of list, from Michael-scott
  3. */
  4. public class Linkedqueue<e> {
  5. private static Class node<e>{
  6. final E item;
  7. final atomicreference<node<e>> next;
  8. Public Node (E item, node<e> next) {
  9. This.item = Item;
  10. this.next = new atomicreference<> (next);
  11. }
  12. }
  13. private final node<e> dummy = new node<e> (null, NULL);
  14. Private final atomicreference<node<e>> head =
  15. new atomicreference<> (dummy);
  16. private final atomicreference<node<e>> tail =
  17. new atomicreference<> (dummy);
  18. Public Boolean put (E item) {
  19. node<e> NewNode = new node<e> (item, NULL);
  20. While (true) {
  21. Node<e> curtail = Tail.get ();
  22. node<e> Tailnext = CurTail.next.get ();
  23. if (curtail = = Tail.get ()) { //Trailer not modified
  24. if (tailnext! = null) {
  25. //queue is in the middle (i.e. the new node has been connected, the tail node has not been updated), push the tail node
  26. Tail.compareandset (curtail, tailnext);
  27. } else{
  28. //In a stable state, try inserting a new node
  29. if (CurTail.next.compareAndSet (null, NewNode)) {
  30. //After insert succeeds, push tail node
  31. Tail.compareandset (curtail, tailnext);
  32. return true;
  33. }
  34. }
  35. }
  36. }
  37. }
  38. }

Java concurrency Programming the 15th Chapter atom variable and non-blocking synchronization mechanism

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.