An example of lock free programming of CAS instruction in Java language

The first contact with the relevant content should start with the volatile keyword. You know that it can ensure the visibility of variables, and use it to realize atomic operations of reading and writing... But volatile can't do anything to implement some composite operations... The most typical representatives are increasing and decreasing operations....

We know that in a concurrent environment, the simplest way to achieve data consistency is to lock to ensure that only one thread can operate on the data at the same time.... For example, a counter can be implemented in the following ways:

We use the synchronized keyword to modify operations to ensure synchronous access to attribute a... In this way, the consistency of a in the concurrent environment can be guaranteed, but due to the use of locks, lock overhead, thread scheduling and so on, the scalability of the program will be limited, so there are many lock free implementation methods....

In fact, these lock free methods use some CAS (compare and switch) instructions provided by the processor. What exactly does this CAS do? You can use the following method to explain the semantics represented by CAS:

Well, the code should be clear about the CAS semantics. It seems that most processors now implement atomic CAS instructions.. Well, let's look at where CAS is used in Java. First, let's look at the atomicinteger type, which is a type provided in the concurrency Library:

This is an internally defined attribute used to save values. Because it is of volatile type, it can ensure the visibility between threads and the atomicity of reading and writing... Next, let's take a look at some common methods:

The function of this method is to add delta to the current value. It can be seen that there is no lock in the whole method. This code is actually a method to implement the lock free counter in Java. The compareandset method is defined as follows:

Since the unsafe method is called, there is nothing I can do. In fact, I can guess that the JVM calls the CAS instruction of the processor itself to realize atomic operation...

Basically, the important methods of atomicinteger type are implemented in a lock free manner.. Therefore, in a concurrent environment, using this type can have better performance... The above is the completion of the implementation of the lockless counter in Java. Next, let's see how to implement the lockless stack and paste the code directly. The code is imitated from the practice of Java Concurrent Programming:

Well, even if the above code implements a lock free stack, it's simple... In a concurrent environment, the scalability of data structures without locks can be much better than that with locks... When it comes to lock free programming, we have to mention lock free queue. In fact, the implementation of lock free queue: concurrentlinkedqueue has been provided in the concurrent library. Let's take a look at its important implementation methods:

This method is used to add elements at the end of the queue. You can see that there is no lock. For the specific lock free algorithm, the non blocking linked list link algorithm proposed by Michael Scott is used... How does it look? You can see it in the actual battle of Java Concurrent Programming. There is a more detailed introduction.

In addition, other methods are actually implemented in a lock free manner.

Finally, in the actual programming, it is better to adopt these lock free implementations in the concurrent environment. After all, it has better scalability.

summary

The above is a full introduction of the lock free programming implementation examples of CAS instructions in Java language. I hope it will be helpful to you!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>