您好,欢迎访问一九零五行业门户网

java并发编程(8)原子变量和非阻塞的同步机制

原子变量和非阻塞的同步机制 一、锁的劣势1.在多线程下:锁的挂起和恢复等过程存在着很大的开销(及时现代的jvm会判断何时使用挂起,何时自旋等待)
2.volatile:轻量级别的同步机制,但是不能用于构建原子复合操作
因此:需要有一种方式,在管理线程之间的竞争时有一种粒度更细的方式,类似与volatile的机制,同时还要支持原子更新操作
二、cas独占锁是一种悲观的技术--它假设最坏的情况,所以每个线程是独占的
而cas比较并交换:compareandswap/set(a,b):我们认为内存处值是a,如果是a,将其修改为b,否则不进行操作;返回内存处的原始值或是否修改成功
如:模拟cas操作 
//模拟的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提供了cas的操作
原子状态类:atomicxxx的cas方法
java7/8:对map的操作:putifabsent、computerifabsent、computerifpresent.........
三、原子变量类 atomicrefence原子更新对象,可以是自定义的对象;如:
public class casnumberrange {private static class intpair {// invariant: lower <= upperfinal int lower; //将值定义为不可变域final int upper; //将值定义为不可变域public intpair(int lower, int upper) {this.lower = lower;this.upper = upper; } }private final atomicreference<intpair> 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 < oldv.lower)throw new illegalargumentexception("can't set upper to " + i + " < lower"); intpair newv = new intpair(oldv.lower, i);if (values.compareandset(oldv, newv))return; } } }
性能问题:使用原子变量在中低并发(竞争)下,比使用锁速度要快,一般情况下是比锁速度快的
四、非阻塞算法许多常见的数据结构中都可以使用非阻塞算法
非阻塞算法:在多线程中,工作是否成功有不确定性,需要循环执行,并通过cas进行原子操作
1、上面的casnumberrange
2、栈的非阻塞算法:只保存头部指针,只有一个状态
//栈实现的非阻塞算法:单向链表public class concurrentstack <e> {     atomicreference<node<e>> top = new atomicreference<node<e>>();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;         }     } }
3、链表的非阻塞算法:头部和尾部的快速访问,保存两个状态,更加复杂
public class linkedqueue <e> {private static class node <e> {final e item;final atomicreference next;public node(e item, linkedqueue.node<e> next) {this.item = item;this.next = new atomicreference(next);         }     }private final linkedqueue.node<e> dummy = new linkedqueue.node<e>(null, null);private final atomicreference head = new atomicreference(dummy);private final atomicreference tail = new atomicreference(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;                     }                 }             }         }     } }
3.原子域更新器
上面的逻辑,实现了链表的非阻塞算法,使用node来保存头结点和尾节点
在实际的concurrentlinkedqueue中使用的是基于反射的atomicreferencefiledupdater来包装node
五、aba问题cas操作中容易出现的问题:
判断值是否为a,是的话就继续更新操作换为b;
但是如果一个线程将值a改为c,然后又改回a,此时,原线程将判断a=a成功执行更新操作;
如果把a改为c,然后又改回a的操作,也需要视为变化,则需要对算法进行优化
解决:添加版本号,每次更新操作都要更新版本号,即使值是一样的
以上就是java并发编程(8)原子变量和非阻塞的同步机制的详细内容。
其它类似信息

推荐信息