package com.concurrent;
import java.lang.reflect.Field;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;
import sun.misc.Unsafe;
/**
* 1,
*/
public class MyLock {
private volatile int status = 0;
private Thread holdThread = null;
private static Unsafe unsafe;
private static long statusOffset;
private final ConcurrentLinkedDeque<Thread> list = new ConcurrentLinkedDeque<>();
/**
* 加锁操作
*/
public void lock() {
// t1,t2,t3 一起操作
if (acquire()) {
return;
}
Thread current = Thread.currentThread();
this.list.add(current);
for (; ; ) {
if (current == list.peek() && acquire()) { //是当前线程并且拿到锁
list.poll();//移除
return;
}
LockSupport.park();//没拿到锁的阻塞
}
}
/**
* 释放锁
*/
public void unlock() {
if (Thread.currentThread() != holdThread) {
throw new RuntimeException("不可以释放");
}
setStatus(0);
int state = getStatus();
if (compareAndSetInt(0, state)) {// 释放锁
setHoldThread(null);
Thread peek = list.peek();
if (peek != null) {
LockSupport.unpark(peek);
}
}
}
public boolean acquire() {
ConcurrentLinkedDeque<Thread> queue = this.list;
int state = getStatus();
Thread currentThread = Thread.currentThread();
if (state == 0) {
if ((queue.size() == 0 || queue.peek() == currentThread) && compareAndSetInt(0, 1)) {
setHoldThread(Thread.currentThread());
return true;
}
}
return false;
}
public boolean compareAndSetInt(int expect, int update) {
return unsafe.compareAndSwapInt(this, statusOffset, expect, update);
}
static {
Field field = null;
try {
field = Unsafe.class.getDeclaredField("theUnsafe");
//因为 Unsafe 的 theUnsafe 字段是private 的,所以这里需要设置成可访问的
field.setAccessible(true);
//Unsafe 的这个属性 theUnsafe 是静态的所以这里的get参数就是null
unsafe = (Unsafe) field.get(null);
statusOffset = unsafe.objectFieldOffset(MyLock.class.getDeclaredField("status"));
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public Thread getHoldThread() {
return holdThread;
}
public void setHoldThread(Thread holdThread) {
this.holdThread = holdThread;
}
static Integer count = 0;
static CountDownLatch countDownLatch = new CountDownLatch(100);
public static void main(String[] args) throws InterruptedException {
ExecutorService executorService = Executors.newCachedThreadPool();
final MyLock myLock = new MyLock();
for (int i = 0; i < 100; i++) {
executorService.execute(new Runnable() {
@Override
public void run() {
try {
myLock.lock();
for (int j = 0; j < 100; j++) {
count ++;
}
countDownLatch.countDown();
} catch (Exception e) {
e.printStackTrace();
}finally {
myLock.unlock();
}
}
});
}
countDownLatch.await();
System.err.println("count: " + count);
new ReentrantLock();
}
}
CAS锁
最新推荐文章于 2025-05-08 12:25:23 发布