目录
java 里有两种锁
-
synchronized (jvm内部实现)
-
JUC( java.util.concurrent由jdk源码实现)
那么Doug Lea是怎样写的同步锁呢?
Doug lea就是写出JUC的一个大佬。
我们先了解一些要用到的知识:
CAS:全称compare and swap(比较交换),比较三个值,内存里的值(O)、预期值(E)、新值(N),在进行CAS操作时,会比较O 和 E 是否相等,相等就把N的值赋值给O,可以理解为修改密码,数据库值(O),验证旧密码(E),新密码(N)验证通过,修改成功。
UNSAFE:给jdk提供最底层的方法,包括volatile、线程调度、CAS相关、内存相关等功能。
尝试写一个同步锁,可能的方式如下:
1.使用自旋
2.sleep+自旋
3.yield+自旋
4.park+自旋
下面使用了AtomicInteger类,他也是Doug lea写的,底层调用unsafe类实现,即借用它来实现。
第一种方案:自旋
public class SlipLock {
// 0:无锁
private volatile AtomicInteger status = new AtomicInteger(0);
public void lock(){
while (!status.compareAndSet(0, 1)) {
}
}
public void unlock(){
status.set(0);
}
}
public static void spinLockTest(){
List<Thread> list = new ArrayList<>();
SlipLock lock = new SlipLock();
for (int i = 0; i < 10; i++) {
Thread t1 = new Thread(()->{
lock.lock();
for (int j = 0; j < 10; j++) {
System.out.print(Thread.currentThread().getName()+" "+j);
}
System.out.println();
lock.unlock();
},"线程-"+i);
list.add(t1);
}
list.forEach(t->t.start());
}
第二种方案:yield+自旋
public class YieldLock {
// 0:无锁
private AtomicInteger status = new AtomicInteger(0);
public void lock(){
while (!status.compareAndSet(0, 1)) {
Thread.yield();
}
}
public void unlock(){
status.set(0);
}
}
public static void yieldLockTest() {
List<Thread> list = new ArrayList<>();
YieldLock lock = new YieldLock();
for (int i = 0; i < 10; i++) {
Thread t1 = new Thread(()->{
lock.lock();
for (int j = 0; j < 10; j++) {
System.out.print(Thread.currentThread().getName()+" "+j);
}
System.out.println();
lock.unlock();
},"线程-"+i);
list.add(t1);
}
list.forEach(t->t.start());
}
第三种方案:sleep+自旋
public class SleepLock {
// 0:无锁
private volatile AtomicInteger status = new AtomicInteger(0);
public void lock(){
while (!status.compareAndSet(0, 1)) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void unlock(){
status.set(0);
}
}
public static void sleepLockTest() {
List<Thread> list = new ArrayList<>();
SleepLock lock = new SleepLock();
for (int i = 0; i < 10; i++) {
Thread t1 = new Thread(()->{
lock.lock();
for (int j = 0; j < 10; j++) {
System.out.print(Thread.currentThread().getName()+" "+j);
}
System.out.println();
lock.unlock();
},"线程-"+i);
list.add(t1);
}
list.forEach(t->t.start());
}
第四种方案:park
unsafe提供给jdk底层使用,当我们使用时会坚持是否时“受信任”的类,jdk也提供了第三方工具类LockSupport;
这里park会阻塞线程,然后再调用unpark的时候,从park方法出继续执行;这样的方式使得线程按排队时间长短获取锁,比上面的方法更为公平。
public class ParkLook {
// 0:无锁
private AtomicInteger status = new AtomicInteger(0);
// 装载等待的线程
private List<Thread> list = new ArrayList<>();
public void lock(){
while (!status.compareAndSet(0, 1)) {
list.add(Thread.currentThread());
LockSupport.park();
}
}
public void unlock(){
status.compareAndSet(1,0);
if(list.size() > 0) {
Thread thread = list.get(0);
list.remove(0);
LockSupport.unpark(thread);
}
}
}
public static void parkLockTest(){
List<Thread> list = new ArrayList<>();
ParkLook lock = new ParkLook();
for (int i = 0; i < 10; i++) {
Thread t1 = new Thread(()->{
lock.lock();
for (int j = 0; j < 10; j++) {
System.out.print(Thread.currentThread().getName()+" "+j);
}
System.out.println();
lock.unlock();
},"线程-"+i);
list.add(t1);
}
list.forEach(t->t.start());
}
在最后一个方法中就已经能够看出AQS的一个样貌了,那么什么是AQS呢?
AbstractQueuedSynchronizer简称AQS,Doug lea设计的提供实现阻塞锁和一些列依赖FIFO等待队列的同步器框架。在synchronized优化之前Doug lea写的这个并发框架性能是很高的,小米首席架构师崔宝秋说过:要多看优秀的代码,这样才能写出优秀的代码。下一篇我们就慢慢看源码。