public class Test { public static void main(String[] args){ synchronized(Test.class){ System.out.println("hello"); } }}
截取部分字节码,如下
4: monitorenter 5: getstatic #9 // Field java/lang/System.out:Ljava/io/PrintStream; 8: ldc #15 // String hello 10: invokevirtual #17 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 13: aload_1 14: monitorexit
字节码出现了4: monitorenter和14: monitorexit两个指令;字面理解就是监视进入,监视退出。可以理解为代码块执行前的加锁,和退出同步时的解锁
objectMonitor.cpp ObjectMonitor() { _header = NULL; _count = 0; 用来记录获取该锁的线程数 _waiters = 0, _recursions = 0; 锁的重入次数 _object = NULL; _owner = NULL; 当前持有ObjectMonitor的线程 _WaitSet = NULL; wait()方法调用后的线程等待队列 _WaitSetLock = 0 ; _Responsible = NULL ; _succ = NULL ; _cxq = NULL ; 阻塞等待队列 FreeNext = NULL ; _EntryList = NULL ; synchronized 进来线程的排队队列 _SpinFreq = 0 ; _SpinClock = 0 ; 自旋计算 OwnerIsThread = 0 ; }
void ATTR ObjectMonitor::enter(TRAPS) { ... //获取锁:cmpxchg_ptr原子操作,尝试将_owner替换为自己,并返回旧值 cur = Atomic::cmpxchg_ptr (Self, &_owner, NULL) ; ... // 重复获取锁,次数加1,返回 if (cur == Self) { _recursions ++ ; return ; } //首次获取锁情况处理 if (Self->is_lock_owned ((address)cur)) { assert (_recursions == 0, "internal state error"); _recursions = 1 ; _owner = Self ; OwnerIsThread = 1 ; return ; } ... //尝试自旋获取锁 if (Knob_SpinEarly && TrySpin (Self) > 0) { ...
总结
public synchronized void lock(){ System.out.println("world"); }.... public synchronized void lock(); descriptor: ()V flags: (0x0029) ACC_PUBLIC, ACC_SYNCHRONIZED Code: stack=2, locals=0, args_size=0 0: getstatic #20 // Field java/lang/System.out:Ljava/io/PrintStream; 3: ldc #26 // String world 5: invokevirtual #28 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
class ObjectWaiter : public StackObj { public: enum TStates { TS_UNDEF, TS_READY, TS_RUN, TS_WAIT, TS_ENTER, TS_CXQ } ; enum Sorted { PREPEND, APPEND, SORTED } ; ObjectWaiter * volatile _next; ObjectWaiter * volatile _prev; Thread* _thread; ParkEvent * _event; volatile int _notified ; volatile TStates TState ; Sorted _Sorted ; // List placement disposition bool _active ; // Contention monitoring is enabled public: ObjectWaiter(Thread* thread); void wait_reenter_begin(ObjectMonitor *mon); void wait_reenter_end(ObjectMonitor *mon);};
调用对象锁的wait()方法时,线程会被封装成ObjectWaiter,最后使用park方法挂起
//objectMonitor.cppvoid ObjectMonitor::wait(jlong millis, bool interruptible, TRAPS){ ... //线程封装成 ObjectWaiter对象 ObjectWaiter node(Self); node.TState &#61; ObjectWaiter::TS_WAIT ; ... //一系列判断操作&#xff0c;当线程确实加入WaitSet时&#xff0c;则使用park方法挂起 if (node._notified &#61;&#61; 0) { if (millis <&#61; 0) { Self->_ParkEvent->park () ; } else { ret &#61; Self->_ParkEvent->park (millis) ; } }
而当对象锁使用notify()时
void ObjectMonitor::notify(TRAPS){ CHECK_OWNER(); //waitSet为空&#xff0c;则直接返回 if (_WaitSet &#61;&#61; NULL) { TEVENT (Empty-Notify) ; return ; } ... //通过DequeueWaiter获取_WaitSet列表中的第一个ObjectWaiter Thread::SpinAcquire (&_WaitSetLock, "WaitSet - notify") ; ObjectWaiter * iterator &#61; DequeueWaiter() ; if (iterator !&#61; NULL) { .... if (Policy &#61;&#61; 2) { // prepend to cxq // prepend to cxq if (List &#61;&#61; NULL) { iterator->_next &#61; iterator->_prev &#61; NULL ; _EntryList &#61; iterator ; } else { iterator->TState &#61; ObjectWaiter::TS_CXQ ; for (;;) { ObjectWaiter * Front &#61; _cxq ; iterator->_next &#61; Front ; if (Atomic::cmpxchg_ptr (iterator, &_cxq, Front) &#61;&#61; Front) { break ; } } } }
Test.class//编译器会考虑将两次加锁合并public void test(){ synchronized(this){ System.out.println("hello"); } synchronized(this){ System.out.println("world"); }}
//StringBuffer的append操作会加上synchronized&#xff0c;//但是变量buf不加锁也安全的&#xff0c;编译器会把锁消除public void test() { StringBuffer buf &#61; new StringBuffer(); buf.append("hello").append("world");}
int expectedValue &#61; 1;public boolean compareAndSet(int newValue) { if(expectedValue &#61;&#61; 1){ expectedValue &#61; newValue; return ture; } return false;}
在jdk是有提供同步版的CAS解决方案&#xff0c;其中使用了UnSafe.java的底层方法
//UnSafe.java &#64;HotSpotIntrinsicCandidate public final native boolean compareAndSetInt(Object o, long offset, int expected, int x) .. &#64;HotSpotIntrinsicCandidate public final native int compareAndExchangeInt(Object o, long offset, int expected, int x)...
我们再来看看本地方法&#xff0c;Unsafe.cpp中的compareAndSwapInt
//unsafe.cppUNSAFE_ENTRY(jboolean, Unsafe_CompareAndSwapInt(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jint e, jint x)) UnsafeWrapper("Unsafe_CompareAndSwapInt"); oop p &#61; JNIHandles::resolve(obj); jint* addr &#61; (jint *) index_oop_from_field_offset_long(p, offset); return (jint)(Atomic::cmpxchg(x, addr, e)) &#61;&#61; e;UNSAFE_END
在Linux的x86&#xff0c;Atomic::cmpxchg方法的实现如下
/** 1 __asm__表示汇编的开始&#xff1b; 2 volatile表示禁止编译器优化&#xff1b;//禁止指令重排 3 LOCK_IF_MP是个内联函数&#xff0c; 根据当前系统是否为多核处理器&#xff0c; 决定是否为cmpxchg指令添加lock前缀 //内存屏障*/inline jint Atomic::cmpxchg (jint exchange_value, volatile jint* dest, jint compare_value) { int mp &#61; os::is_MP(); __asm__ volatile (LOCK_IF_MP(%4) "cmpxchgl %1,(%3)" : "&#61;a" (exchange_value) : "r" (exchange_value), "a" (compare_value), "r" (dest), "r" (mp) : "cc", "memory"); return exchange_value;}
到这一步&#xff0c;可以总结到&#xff1a;jdk提供的CAS机制&#xff0c;在汇编层级&#xff0c;会禁止变量两侧的指令优化&#xff0c;然后使用cmpxchg指令比较并更新变量值(原子性)&#xff0c;如果是多核则使用lock锁定(缓存锁、MESI)
//伪代码volatile state &#61; 0 ; // 0-无锁 1-加锁&#xff1b;volatile禁止指令重排&#xff0c;加入内存屏障...if(cas(state, 0 , 1)){ // 1 加锁成功&#xff0c;只有一个线程能成功加锁 ... // 2 同步代码块 cas(state, 1, 0); // 3 解锁时2的操作具有可见性}
JavaThread* thread&#61;JavaThread::thread_from_jni_environment(env);...thread->parker()->park(isAbsolute !&#61; 0, time);
class PlatformParker : public CHeapObj { protected: //互斥变量类型 pthread_mutex_t _mutex [1] ; //条件变量类型 pthread_cond_t _cond [1] ; ...}class Parker : public os::PlatformParker { private: volatile int _counter ; ... public: void park(bool isAbsolute, jlong time); void unpark(); ... }
//AbstractQueuedSynchronizer.javapublic class AbstractQueuedSynchronizer{ //线程节点 static final class Node { ... volatile Node prev; volatile Node next; volatile Thread thread; ... } .... //head 等待队列头尾节点 private transient volatile Node head; private transient volatile Node tail; // The synchronization state. 同步状态 private volatile int state; ... //提供CAS操作&#xff0c;状态具体的修改由子类实现 protected final boolean compareAndSetState(int expect, int update) { return STATE.compareAndSet(this, expect, update); }}
public final void acquire(int arg) { if (!tryAcquire(arg) && acquireQueued(addWaiter(Node.EXCLUSIVE), arg)) selfInterrupt();}
在AQS还存一个ConditionObject的内部类&#xff0c;它的使用机制和Object.wait、notify类似
//AbstractQueuedSynchronizer.javapublic class ConditionObject implements Condition, java.io.Serializable { //条件队列;Node 复用了AQS中定义的Node private transient Node firstWaiter; private transient Node lastWaiter; ...
//类似Object.waitpublic final void await() throws InterruptedException{ ... Node node &#61; addConditionWaiter(); //构造Node,加入条件队列 int savedState &#61; fullyRelease(node); int interruptMode &#61; 0; while (!isOnSyncQueue(node)) { //挂起线程 LockSupport.park(this); if ((interruptMode &#61; checkInterruptWhileWaiting(node)) !&#61; 0) break; } //notify唤醒线程后&#xff0c;加入同步队列继续竞争锁 if (acquireQueued(node, savedState) && interruptMode !&#61; THROW_IE) interruptMode &#61; REINTERRUPT;
//类似Object.notify private void doSignal(Node first) { do { if ( (firstWaiter &#61; first.nextWaiter) &#61;&#61; null) lastWaiter &#61; null; first.nextWaiter &#61; null; } while (!transferForSignal(first) && (first &#61; firstWaiter) !&#61; null); }
protected boolean tryAcquire(int arg);//尝试独占性加锁protected boolean tryRelease(int arg);//对应tryAcquire释放锁protected int tryAcquireShared(int arg);//尝试共享性加锁protected boolean tryReleaseShared(int arg);//对应tryAcquireShared释放锁protected boolean isHeldExclusively();//该线程是否正在独占资源&#xff0c;只有用到condition才需要取实现它
abstract static class Sync extends AbstractQueuedSynchronizer{ .... final boolean nonfairTryAcquire(int acquires) { final Thread current &#61; Thread.currentThread(); int c &#61; getState(); if (c &#61;&#61; 0) { //直接CAS状态加锁&#xff0c;非公平操作 if (compareAndSetState(0, acquires)) { setExclusiveOwnerThread(current); return true; } } ... //重写了tryRelease protected final boolean tryRelease(int releases) { c &#61; state - releases; //改变同步状态 ... //修改volatile 修饰的状态变量 setState(c); return free; }}
static final class NonfairSync extends Sync { protected final boolean tryAcquire(int acquires) { return nonfairTryAcquire(acquires); } } .... static final class FairSync extends Sync { protected final boolean tryAcquire(int acquires) { final Thread current &#61; Thread.currentThread(); int c &#61; getState(); if (c &#61;&#61; 0) { if (!hasQueuedPredecessors() && compareAndSetState(0, acquires)) { setExclusiveOwnerThread(current); return true; } } ....
public class TwinsLock implements Lock { private final Sync sync &#61; new Sync(2); &#64;Override public void lockInterruptibly() throws InterruptedException { throw new RuntimeException(""); } &#64;Override public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {throw new RuntimeException("");} &#64;Override public Condition newCondition() { return sync.newCondition(); } &#64;Override public void lock() { sync.acquireShared(1); } &#64;Override public void unlock() { sync.releaseShared(1); } } &#64;Override public boolean tryLock() { return sync.tryAcquireShared(1) > -1; }}
再来看看Sync的代码
class Sync extends AbstractQueuedSynchronizer { Sync(int count) { if (count <&#61; 0) { throw new IllegalArgumentException("count must large than zero"); } setState(count); } &#64;Override public int tryAcquireShared(int reduceCount) { for (; ; ) { int current &#61; getState(); int newCount &#61; current - reduceCount; if (newCount <0 || compareAndSetState(current, newCount)) { return newCount; } } } &#64;Override public boolean tryReleaseShared(int returnCount) { for (; ; ) { int current &#61; getState(); int newCount &#61; current &#43; returnCount; if (compareAndSetState(current, newCount)) { return true; } } } public Condition newCondition() { return new AbstractQueuedSynchronizer.ConditionObject(); } }
作者&#xff1a;clswcl
链接&#xff1a;https://juejin.im/post/5f13f2235188252e362e2e97
来源&#xff1a;掘金
著作权归作者所有。商业转载请联系作者获得授权&#xff0c;非商业转载请注明出处。