


Atomic variables and non-blocking synchronization mechanism
1. Disadvantages of locks
1. Under multi-threading: there is a lot of overhead in the process of lock suspension and recovery ( Modern jvm will determine when to use suspend and when to spin and wait)
2.volatile: lightweight level synchronization mechanism, but cannot be used to build atomic composite operations
Therefore : There needs to be a way to manage competition between threads with a finer granularity, similar to the volatile mechanism, while also supporting atomic update operations
2. CAS
Exclusive locking is a pessimistic technique - it assumes the worst case scenario, so each thread is exclusive
And CAS compare and swap: compareAndSwap/Set(A,B): We think the value in memory It is A. If it is A, modify it to B, otherwise no operation will be performed; return the original value in the memory or whether the modification is successful
For example: simulate CAS operation
//模拟的CASpublic class SimulatedCAS {private int value;public synchronized int get() {return value; }//CAS操作public synchronized int compareAndSwap(int expectedValue, int newValue) {int oldValue = value;if (oldValue == expectedValue) { value = newValue; }return oldValue; }public synchronized boolean compareAndSet(int expectedValue, int newValue) {return (expectedValue == compareAndSwap(expectedValue, newValue)); } }//典型使用场景public class CasCounter {private SimulatedCAS value;public int getValue() {return value.get(); }public int increment() {int v;do { v = value.get(); } while { (v != value.compareAndSwap(v, v + 1)); }return v + 1; } }
JAVA provides CAS operations
Atomic state class: CAS method of AtomicXXX
JAVA7/8: Map operations: putIfAbsent, computerIfAbsent, computerIfPresent.. .......
3. Atomic variable class
AtomicRefence atomic update object can be a custom object; such as:
public class CasNumberRange {private static class IntPair {// INVARIANT: lower values = new AtomicReference<intpair>(new IntPair(0, 0)); //封装对象public int getLower() {return values.get().lower; }public int getUpper() {return values.get().upper; }public void setLower(int i) {while (true) { IntPair oldv = values.get();if (i > oldv.upper) {throw new IllegalArgumentException("Can't set lower to " + i + " > upper"); } IntPair newv = new IntPair(i, oldv.upper); //属性为不可变域,则每次更新新建对象if (values.compareAndSet(oldv, newv)) { //原子更新,如果在过程中有线程修改了,则其他线程不会更新成功,因为oldv与内存处值就不同了return; } } }//同上public void setUpper(int i) {while (true) { IntPair oldv = values.get();if (i </intpair>
Performance issue: Using atomic variables is faster than using locks under medium and low concurrency (competition). Generally, it is faster than locking speed
4. Non-blocking algorithm
Non-blocking algorithm can be used in many common data structures
Non-blocking algorithm: In multi-threading, there is uncertainty about whether the work will be successful and it needs to be executed in a loop. And perform atomic operations through CAS
1. The above CasNumberRange
2. Non-blocking algorithm of the stack: only save the head pointer, only one state
//栈实现的非阻塞算法:单向链表public class ConcurrentStack <e> { AtomicReference<node>> top = new AtomicReference<node>>();public void push(E item) { Node<e> newHead = new Node<e>(item); Node<e> oldHead;do { oldHead = top.get(); newHead.next = oldHead; } while (!top.compareAndSet(oldHead, newHead));//CAS操作:原子更新操作,循环判断,非阻塞 }public E pop() { Node<e> oldHead; Node<e> newHead;do { oldHead = top.get();if (oldHead == null) {return null; } newHead = oldHead.next; } while (!top.compareAndSet(oldHead, newHead));//CAS操作:原子更新操作,循环判断,非阻塞return oldHead.item; }private static class Node <e> {public final E item;public Node<e> next;public Node(E item) {this.item = item; } } }</e></e></e></e></e></e></e></node></node></e>
3. Non-blocking algorithm for linked lists: fast access to the head and tail, saving two states, more complex
public class LinkedQueue <e> {private static class Node <e> {final E item;final AtomicReference<linkedqueue.node>> next;public Node(E item, LinkedQueue.Node<e> next) {this.item = item;this.next = new AtomicReference<linkedqueue.node>>(next); } }private final LinkedQueue.Node<e> dummy = new LinkedQueue.Node<e>(null, null);private final AtomicReference<linkedqueue.node>> head = new AtomicReference<linkedqueue.node>>(dummy);private final AtomicReference<linkedqueue.node>> tail = new AtomicReference<linkedqueue.node>>(dummy); //保存尾节点public boolean put(E item) { LinkedQueue.Node<e> newNode = new LinkedQueue.Node<e>(item, null);while (true) { LinkedQueue.Node<e> curTail = tail.get(); LinkedQueue.Node<e> tailNext = curTail.next.get();if (curTail == tail.get()) {if (tailNext != null) {// 处于中间状态,更新尾节点为当前尾节点的next tail.compareAndSet(curTail, tailNext); } else {// 将当前尾节点的next 设置为新节点:链表if (curTail.next.compareAndSet(null, newNode)) {/** * 此处即为中间状态,虽然在这里进行了两次原子操作,整体不是原子的,但是通过算法保证了安全: * 原因是处于中间状态时,如果有其他线程进来操作,则上面那个if将执行; * 上面if的操作是来帮助当前线程完成更新尾节点操作,而当前线程的更新就会失败返回,最终则是更新成功 */// 链接成功,尾节点已经改变,则将当前尾节点,设置为新节点 tail.compareAndSet(curTail, newNode);return true; } } } } } }</e></e></e></e></linkedqueue.node></linkedqueue.node></linkedqueue.node></linkedqueue.node></e></e></linkedqueue.node></e></linkedqueue.node></e></e>
3. Atomic domain updater
The above logic implements the non-blocking algorithm of the linked list, using Node to save the head node and tail node
The actual ConcurrentLinkedQueue is based on Reflected AtomicReferenceFiledUpdater to wrap Node
5. ABA problem
Issues that are likely to occur in CAS operations:
Judgment value Whether it is A, if so, continue the update operation and change it to B;
But if a thread changes the value A to C, and then changes it back to A, at this time, the original thread will judge that A=A and successfully execute the update Operation;
If you change A to C and then change it back to A, it also needs to be regarded as a change, and the algorithm needs to be optimized
Solution: Add a version number every time it is updated All operations must update the version number, even if the value is the same
The above is the detailed content of Java concurrent programming (8) Atomic variables and non-blocking synchronization mechanism. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version
Chinese version, very easy to use

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version
SublimeText3 Linux latest version