Paip. Improve Performance ---- java lock-free structure (CAS, Atomic, Threadlocal, volatile, Function

Source: Internet
Author: User

Paip. Improved performance ---- java lock-free structure (CAS, Atomic, Threadlocal, volatile, function-based encoding, unchanged object)

 

1. disadvantages of the lock... 1

2 CAS (CompareAnd Swap/Set) operation automic data type AtomicLong, AtomicReference (CAS operation provided by Java) 1

3 Threadlocal2

4. MemoryBarries and javavolatile replace the traditional lock. 2

5. Disruptor (No lockConcurrency framework) 4

6 Peterson algorithm... 4

7. Function encoding .... 4

8. Local Resource replication and asynchronous processing .... 4

9. Immutableobject unchanged object... 5

10 reference... 5

 

Author Attilax airlong, EMAIL: 1466519819@qq.com
Source: attilax Column
Address: http://blog.csdn.net/attilax

Disadvantages of 1 lock 2 CAS (Compare And Swap/Set) operation automic data type AtomicLong, AtomicReference(CAS operations provided by Java)

This is a CPU-level command. In my consciousness, it works a bit like an optimistic lock-the CPU updates a value, but if the value to be changed is no longer the original value, the operation fails because it is obvious that other operations change the value first.

 

Changed the value.

 

Note that this can be two different CPU cores, but not two independent CPUs.

CAS operations consume less resources than locks because they do not involve the operating system and they operate directly on the CPU. But they do not have no cost-in the above test, the lock-free time for a single thread is 300 ms, the lock time for a single thread is 10000 ms, and the CAS time for a single thread is 5700 ms. Therefore, it takes less time to use the lock than to use the lock, but is more time-consuming than a single thread that does not need to consider competition.

 

 

3 Threadlocal

No competition = No lock = very fast.

 

Advantages of Disruptor over traditional methods:

  1. No competition = No lock = very fast. All visitors record their serial numbers and allow multiple producers to share the same data structure with multiple consumers. The serial number (ring buffer, claim Strategy, producer and consumer) can be tracked in each object. In addition, the magic cache line padding means that there is no pseudo-sharing or unexpected competition.

     

    4. Memory Barries and java volatile replace traditional locks

    Volatile isLightweight synchronized...Volatile variable modifier if you useAppropriateIt is better than synchronized'sLower cost of use and executionBecause it does not cause thread context switching and scheduling.

     

    Starting with the volatile keyword, you can know that it can ensure the visibility of variables and use it to implement atomic operations for reading and writing... But there is no way to implement some composite operations volatile... The most typical examples are incremental and progressive operations...

    It is a CPU command. That's right, again, we're talking about CPU-level things to get the performance we want (Martin's famous Mechanical Sympathy theory ). Basically, it is such a command: a) ensures the order in which certain operations are executed; B) affects the visibility of some data (possibly the result after some commands are executed ).

    The compiler and CPU can re-sort the commands to optimize the performance when the output results are the same. Inserting a memory barrier is equivalent to telling the CPU and compiler to execute this command first, and then the command must be executed later. Just as the order of sites on the journey to Las Vegas is clear in your mind.

     

    Another function of the memory barrier is to forcibly update the cache of different CPUs. For example, a write barrier refreshes the data written before the barrier to the cache, so that any thread trying to read the data will get the latest value, you do not need to consider which cpu core or CPU is used for execution.

     

     

     

    Here is a magic mantra called volatile (I think this word has never been clearly explained in Java specifications ). If your field is volatile, the Java Memory Model inserts a write barrier instruction after the write operation and a read barrier instruction before the read operation.

     

    This means that if you write a volatile field, you must know:

    1. Once you finish writing, any thread accessing this field will get the latest value.

    2. Before writing, you will ensure that all previous events have occurred and any updated data values are visible, because the memory barrier will refresh the previous write values to the cache.

     

    Volatile variable is also one of the reasons why we can implement Disruptor without locking.

     

     

    Impact on Performance

    The memory barrier serves as another CPU-level command, without the overhead of locking. The kernel does not interfere and schedule among multiple threads. But everything has a price. The memory barrier is indeed overhead-the compiler/cpu cannot re-sort the instruction, so it is impossible to use the CPU as efficiently as possible. In addition, refreshing the cache will also overhead. Therefore, do not consider using volatile instead of locking.

     

    The memory barrier is a CPU command that allows you to make assumptions about when data is visible to other processes. In Java, you use the volatile keyword to implement memory barrier. Using volatile means you don't have to be forced to choose to lock, and it also improves your performance.

     

     

    5. Disruptor (No lockConcurrency Framework)6 PetersonAlgorithm

    (Evolution of the Dekker algorithm) This algorithm is cleverly designed. The core of understanding is to figure out how the three flag spaces control the access of the two methods to the critical section:

    7. Function encoding.

    Function encoding is the most natural and efficient lock-free method,

    8. Local Resource replication and asynchronous processing.

    The competition for resources is an important cause of the lock. In many cases, there is only one resource, but for every thread that uses the resource, you can see a copy of its own (this is not a real resource, it is probably just a buffer, each thread uses its own buffer, to a certain extent, the buffer is processed into the unique resource, which reduces the impact of the lock on the thread), without the need to consider concurrent use.

    9. Immutable object unchanged object

    In the past, many lock-independent data structures have achieved thread security through Immutable object, which is similarStringBut the performance is low because too many replication operations are involved.

     

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.