Concurrent1_queue principle (II)

Source: Internet
Author: User

Concurrenthashmap is a concurrent collection class starting with concurrent. Its principle is to increase the concurrency by adding the lock and refining the lock granularity.

The concurrentlinkedqueue class adopts another method to improve the concurrency: non-blocking (non-blocking), which achieves lockless concurrency for the first time..

Here, we will first introduce the non-blocking algorithm. In fact, non-blocking algorithms are not mysterious and profound. They need a set of hardware and commands (which seems to be supported by most PCs ),

The main solution is: in many cases, a thread a holds the resources required by other threads B, C, and D, but thread a encounters network congestion or database connection congestion, or page blocking.

In this case, B, C, and D must wait until the execution of a is over before moving forward. This situation often occurs in data structures such as queues and stacks. A typical example is to extract data from a queue and lock the entire queue,

That is to say, in queue operations, each thread is actually serial, and the overhead of thread context switching needs to be added in the middle. How to Improve the concurrency step by step when retrieving elements in the queue (just like concurrenthashmap, only lock part ).

Concurrenthashmap is a fixed 16-segment, and the operations of each segment are independent. Therefore, each segment uses a lock. This also takes into account some overhead and security issues,

The elements in the queue can grow dynamically, because the queue pointer is involved, and it can be atomic without locking a single element. The so-called non-blocking algorithm is a better choice.

Non-Blocking Algorithm

In concurrency in practice, the nonblocking and lock-free concepts are explained. Nonblocking is defined as: failure or suspension of any thread without affecting other threads;

Lock-free is defined as: every step of execution has a thread that can push forward. However, an algorithm based on CAS (compareandset) and correctly constructed must be nonblocking and lock-free.

For non-Blocking Algorithms in Java, the core principle is to use hardware-level commands to ensure the atomicity of CAS. Unlike the pessimistic locking like lock, non-blocking algorithms are optimistic,

It is insecure based on some algorithm steps. It may succeed or fail each time it performs cas. If it fails, it will take the new value and re-cas,

In this way, you do not need to use lock every time to ensure that the thread to be locked must succeed.

A good example is the theory and practice of Java: nonblocking stack in the introduction to non-blocking algorithms. Here we use the non-Blocking Algorithm of Treiber.

This example is relatively easy. Next we will introduce the PUT Method of concurrent1_queue. Here we use the Michael-Scott algorithm.

Developing a non-blocking algorithm is a very challenging task. Every step of an algorithm must prove that there will be no conflicts or deadlocks.

Of course, there are also some rules. First, whether or not the multi-threaded Multi-step execution must make the data structure always consistent.

That is, one thread cannot interrupt the atomic operation of another thread. Secondly, assume that one thread executes the update and the other thread waits for the update,

If the previous thread fails to update, the latter thread will waste time waiting and will not push forward.

The solution is to refine the atomic operation granularity, And the next thread uses snapshots.
@ Threadsafe
Public class upload queue <E> {
Private Static class node <E> {
Final e item;
Final atomicreference <node <E> next;
Public node (E item, node <E> next ){
This. Item = item;
This. Next = new atomicreference <node <E> (next );
}
}
Private final node <E> dummy = new node <E> (null, null );
Private Final atomicreference <node <E> head
= New atomicreference <node <E> (dummy );
Private Final atomicreference <node <E> tail
= New atomicreference <node <E> (dummy );
Public Boolean put (E item ){
Node <E> newnode = new node <E> (item, null );
While (true ){
Node <E> curtail = tail. Get ();
Node <E> tailnext = curtail. Next. Get ();
If (curtail = tail. Get ()){
If (tailnext! = NULL ){
Tail. compareandset (curtail, tailnext );
} Else {
If (curtail. Next. compareandset (null, newnode )){
Tail. compareandset (curtail, newnode );
Return true;
}
}
}
}
}
}

Note 1: compareandset here is the atomicreference method.

Full name: Java. util. Concurrent. Atomic. atomicreference <v>

The description in this document is as follows:

Compareandset
Public final Boolean compareandset (v CT,

V update) if the current value is equal to the expected value, the value is set as the specified update value in an atomic manner.
Parameters:

CT-Expected Value

Update-New Value
Return Value:

If yes, true is returned. False indicates that the actual value is not the same as the expected value.

Note 2: The java. util. Concurrent. Atomic package provides nine atomic variables (atomicinteger; atomiclong; atomicreference; atomicboolean;

Atomic integer, long type, reference, and array form of atomic tag reference and stamp reference class, which update a pair of values atomically ).

The atomic variable class can be considered as a generalization of volatile variables. It extends the concept of variable variables to support atomic condition comparison and set updates.

Note 3: For more information about how concurrent1_queue works, see 《Concurrent1_queue principle (Part 1)"

Note 4: For more information about concurrentincluqueue APIs, see 《Concurrent1_queue"

Note 5: For introduction to non-blocking algorithms, refer 《Introduction to Java non-Blocking AlgorithmsAnd 《Popular Atoms"

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.