自己动手写把”锁”---终极篇
public class ThreadList{ private volatile Node head = null; private static long headOffset; private static Unsafe unsafe; static { try { Constructor<Unsafe> constructor = Unsafe.class.getDeclaredConstructor(new Class<?>[0]); constructor.setAccessible(true); unsafe = constructor.newInstance(new Object[0]); headOffset = unsafe.objectFieldOffset(ThreadList.class.getDeclaredField("head")); }catch (Exception e){ } } /** * * @param thread * @return 是否只有当前一个线程在等待 */ public boolean insert(Thread thread){ Node node = new Node(thread); for(;;){ Node first = getHead(); node.setNext(first); if(unsafe.compareAndSwapObject(this, headOffset,first,node)){ return first==null?true:false; } } } public Thread pop(){ Node first = null; for(;;){ first = getHead(); Node next = null; if(first!=null){ next = first.getNext(); } if(unsafe.compareAndSwapObject(this, headOffset,first,next)){ break; } } return first==null?null:first.getThread(); } private Node getHead(){ return this.head; } private static class Node{ volatile Node next; volatile Thread thread; public Node(Thread thread){ this.thread = thread; } public void setNext(Node next){ this.next = next; } public Node getNext(){ return next; } public Thread getThread(){ return this.thread; } } }
public class MyLock { private volatile int state = 0; private ThreadList threadList = new ThreadList(); private static long stateOffset; private static Unsafe unsafe; static { try { Constructor<Unsafe> constructor = Unsafe.class.getDeclaredConstructor(new Class<?>[0]); constructor.setAccessible(true); unsafe = constructor.newInstance(new Object[0]); stateOffset = unsafe.objectFieldOffset(MyLock.class.getDeclaredField("state")); }catch (Exception e){ } } public void lock(){ if(compareAndSetState(0,1)){ }else{ addNodeAndWait(); } } public void unLock(){ compareAndSetState(1,0); Thread thread = threadList.pop(); if(thread != null){ LockSupport.unpark(thread); } } private void addNodeAndWait(){ //如果当前只有一个等待线程时,重新获取一下锁,防止永远不被唤醒。 boolean isOnlyOne = threadList.insert(Thread.currentThread()); if(isOnlyOne && compareAndSetState(0,1)){ return; } LockSupport.park(this);//线程被挂起 if(compareAndSetState(0,1)){//线程被唤醒后继续竞争锁 return; }else{ addNodeAndWait(); } } private boolean compareAndSetState(int expect,int update){ return unsafe.compareAndSwapInt(this,stateOffset,expect,update); } }
public class TestMyLock { private static List<Integer> list = new ArrayList<>(); private static MyLock myLock = new MyLock(); public static void main(String[] args){ Thread t1 = new Thread(new Runnable() { @Override public void run() { for(int i=0;i<10000;i++){ add(i); } } }); Thread t2 = new Thread(new Runnable() { @Override public void run() { print(); } }); t1.start(); t2.start(); } private static void add(int i){ myLock.lock(); list.add(i); myLock.unLock(); } private static void print(){ myLock.lock(); Iterator<Integer> iterator = list.iterator(); while (iterator.hasNext()){ System.out.println(iterator.next()); } myLock.unLock(); } }
boolean isOnlyOne = threadList.insert(Thread.currentThread()); if(isOnlyOne && compareAndSetState(0,1)){ return; }
以下是本系列其他的文章: