luck锁底层
aqs cas lucksuuport
luck
代码语言:javascript复制 public void lock() {
sync.lock();
}
public void unlock() {
sync.release(1);
}
Sync extends AbstractQueuedSynchronizer
手写 luck锁(aqs cas lucksuuport)
代码语言:javascript复制public class GTFlock {
/**
* 0没有获取到
* 1有线程获取到
*/
private AtomicInteger lockState= new AtomicInteger(0);
/**
* 获取到锁的线程
*/
private Thread getLockThread = null;
/**
* 没有获取到的装入链表
*/
private ConcurrentLinkedDeque<Thread> concurrentLinkedDeque=new ConcurrentLinkedDeque<Thread>();
/**
* 获取
*/
public void lock(){
acquire();
}
public boolean acquire(){
for(;;){
if (lockState.compareAndSet(0,1)) {
//获取到锁-成功 记录
getLockThread=Thread.currentThread();
System.out.println("获取到锁-成功 记录");
return true;
}
//获取到锁-失败 装入到链表中
concurrentLinkedDeque.add(Thread.currentThread());
System.out.println("获取到锁-失败 装入到链表中");
//阻塞
LockSupport.park();
}
}
public boolean compareAndSet(int expect,int update){
return lockState.compareAndSet(expect,update);
}
/**
* 释放
*/
public boolean unlock() {
if(getLockThread==null){
return false;
}
//获取到锁的线程 如果为当前线程的话
if (getLockThread == Thread.currentThread()) {
boolean b = compareAndSet(1, 0);
if (b) {
//公平锁
Thread first = concurrentLinkedDeque.getFirst();
LockSupport.unpark(first);
// //非公平锁
// concurrentLinkedDeque.forEach((k)->{
// LockSupport.unpark(k);
// });
}
}
return false;
}
public static void main(String[] args) throws InterruptedException {
GTFlock gtFlock = new GTFlock();
gtFlock.lock();
new Thread(()->{
System.out.println(Thread.currentThread().getName() "start");
gtFlock.lock();
System.out.println(Thread.currentThread().getName() "end");
}).start();
Thread.sleep(1000);
gtFlock.unlock();
}
}
aps
node:底层双向链表 状态:0.1或者大于1,当前线程会重复会获取锁,
lucksuuport基本用法
代码语言:javascript复制 public static void main(String[] args) throws InterruptedException {
Thread t1=new Thread(()->{
System.out.println("start");
//阻塞
LockSupport.park();
System.out.println("end");
}).start();
//唤醒阻塞的线程
LockSupport.unpark(thread);
}
# synchronized锁与luck锁区别
luck锁 升级过程需要自己实现。
cas回顾
代码语言:javascript复制 //cas回顾
private static AtomicInteger atomicInteger = new AtomicInteger(0);
public static void main(String[] args) throws InterruptedException {
boolean b = atomicInteger.compareAndSet(0, 1);
boolean b1 = atomicInteger.compareAndSet(0, 1);
System.out.println(b); //true
System.out.println(b1); //false
}
Semaphore信号量基于aqs限流
代码语言:javascript复制public class Text05 {
public static void main(String[] args) {
//接口限流
Semaphore semaphore = new Semaphore(5);
for (int i = 0; i < 10;i ) {
int factory=i;
new Thread(()->{
try {
semaphore.acquire();
// semaphore.release();
System.out.println(Thread.currentThread().getName() factory);
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
}
}
}
public final void acquireSharedInterruptibly(int arg)
throws InterruptedException {
if (Thread.interrupted())
throw new InterruptedException();
if (tryAcquireShared(arg) < 0)
doAcquireSharedInterruptibly(arg);
}
CountDownLatch
代码语言:javascript复制public class Text05 {
public static void main(String[] args) {
CountDownLatch countDownLatch = new CountDownLatch(2);
new Thread(()->{
try {
//阻塞
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
//唤醒
countDownLatch.countDown();
countDownLatch.countDown();
}
}