java中synchronized是由JVM层实现的锁,而Lock的实现完全是由JAVA层实现,jdk 1.5之前synchronized是重量级锁实现,效率比较低,经过JVM·的优化,jdk1.5版本及后synchronbized的锁效率已经和Lock差不多。
synchronized是通过monitorEnter和monitorExit的JVM指令实现的,
下面通过一个简单demo展示下
public static void main(String[] args) {
synchronized (Test.class){
System.out.println("锁");
}
}
上面的demo的字节码如下,可以看到synchronized关键字的括号包含的代码锁对应的字节码开始前嵌入monitorenter,末尾嵌入monitorexit指令。
public class com.algorithms.interview.template.Test
minor version: 0
major version: 52
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #6.#24 // java/lang/Object."<init>":()V
#2 = Class #25 // com/algorithms/interview/template/Test
#3 = Fieldref #26.#27 // java/lang/System.out:Ljava/io/PrintStream;
#4 = String #28 // 锁
#5 = Methodref #29.#30 // java/io/PrintStream.println:(Ljava/lang/String;)V
#6 = Class #31 // java/lang/Object
#7 = Utf8 <init>
#8 = Utf8 ()V
#9 = Utf8 Code
#10 = Utf8 LineNumberTable
#11 = Utf8 LocalVariableTable
#12 = Utf8 this
#13 = Utf8 Lcom/algorithms/interview/template/Test;
#14 = Utf8 main
#15 = Utf8 ([Ljava/lang/String;)V
#16 = Utf8 args
#17 = Utf8 [Ljava/lang/String;
#18 = Utf8 StackMapTable
#19 = Class #17 // "[Ljava/lang/String;"
#20 = Class #31 // java/lang/Object
#21 = Class #32 // java/lang/Throwable
#22 = Utf8 SourceFile
#23 = Utf8 Test.java
#24 = NameAndType #7:#8 // "<init>":()V
#25 = Utf8 com/algorithms/interview/template/Test
#26 = Class #33 // java/lang/System
#27 = NameAndType #34:#35 // out:Ljava/io/PrintStream;
#28 = Utf8 锁
#29 = Class #36 // java/io/PrintStream
#30 = NameAndType #37:#38 // println:(Ljava/lang/String;)V
#31 = Utf8 java/lang/Object
#32 = Utf8 java/lang/Throwable
#33 = Utf8 java/lang/System
#34 = Utf8 out
#35 = Utf8 Ljava/io/PrintStream;
#36 = Utf8 java/io/PrintStream
#37 = Utf8 println
#38 = Utf8 (Ljava/lang/String;)V
{
public com.algorithms.interview.template.Test();
descriptor: ()V
flags: ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 3: 0
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this Lcom/algorithms/interview/template/Test;
public static void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=2, locals=3, args_size=1
0: ldc #2 // class com/algorithms/interview/template/Test
2: dup
3: astore_1
4: monitorenter
5: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
8: ldc #4 // String 锁
10: invokevirtual #5 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
13: aload_1
14: monitorexit
15: goto 23
18: astore_2
19: aload_1
20: monitorexit
21: aload_2
22: athrow
23: return
Exception table:
from to target type
5 15 18 any
18 21 18 any
LineNumberTable:
line 6: 0
line 7: 5
line 8: 13
line 9: 23
LocalVariableTable:
Start Length Slot Name Signature
0 24 0 args [Ljava/lang/String;
StackMapTable: number_of_entries = 2
frame_type = 255 /* full_frame */
offset_delta = 18
locals = [ class "[Ljava/lang/String;", class java/lang/Object ]
stack = [ class java/lang/Throwable ]
frame_type = 250 /* chop */
offset_delta = 4
}
那么monitorenter和monitorexit指令是如何作用的呢 想要弄清楚这个问题,首先还是得了解Java得对象头(基于64位操作系统)
| Mard Word(64bits) |锁状态|
| unused 25 | identiy_hashcode:31 | unused 1 | age 4|biase_lock 0 |01| 无锁 |
| thradId:54 | epoch:2| unused:1| age:4 | biase_lock 1 |01| 偏向锁|
| ptr_to_lock_record 62 |00| 轻量级锁|
| ptr_to_heavyweight_lock 62 |10| 轻量级锁|
| 空 62 |11| 标记GC |
其中对象头中MarkWord中锁标志位就是表示对象锁的状态的,如上图所示
JVM的源码是如果处理synchronized锁
自从jdk1.5之后,JVM就对synchronized进行了优化,那么它是如何优化成现在 几乎和ReentrantLock性能差不多的呢,那么加下来往下看' 下面是monitorenter和monitorexit指令的经过编译器执行对应的代码的位置 src/hotspot/share/interpreter/interpreterRuntime.cpp
//%note monitor_1
JRT_ENTRY_NO_ASYNC(void, InterpreterRuntime::monitorenter(JavaThread* current, BasicObjectLock* elem))
#ifdef ASSERT
current->last_frame().interpreter_frame_verify_monitor(elem);
#endif
Handle h_obj(current, elem->obj());
assert(Universe::heap()->is_in_or_null(h_obj()),
"must be NULL or an object");
ObjectSynchronizer::enter(h_obj, elem->lock(), current);
assert(Universe::heap()->is_in_or_null(elem->obj()),
"must be NULL or an object");
#ifdef ASSERT
current->last_frame().interpreter_frame_verify_monitor(elem);
#endif
JRT_END
JRT_LEAF(void, InterpreterRuntime::monitorexit(BasicObjectLock* elem))
oop obj = elem->obj();
assert(Universe::heap()->is_in(obj), "must be an object");
// The object could become unlocked through a JNI call, which we have no other checks for.
// Give a fatal message if CheckJNICalls. Otherwise we ignore it.
if (obj->is_unlocked()) {
if (CheckJNICalls) {
fatal("Object has been unlocked by JNI");
}
return;
}
ObjectSynchronizer::exit(obj, elem->lock(), JavaThread::current());
// Free entry. If it is not cleared, the exception handling code will try to unlock the monitor
// again at method exit or in the case of an exception.
elem->set_obj(NULL);
JRT_END
从上面知道monitorenter指令是调用ObjectSynchronizer::enter加锁,对应源码位置是src/hotspot/share/runtime/synchronizer.cpp 首先主要看下enter的加锁的方法
1.判断是否开启偏向锁,handle_sync_on_value_based_class同步处理偏向锁处理
2.如果对象头中MarkWord中没有锁,则先将对象头写入当前线程的栈中,通过CAS将把 MarkWord中对应位置更新为header的指针
3.如果对象头中markword已经存在锁,则通过inflate进行膨胀,尝试获取重量级锁, (即使用ObjectMonitor进行加锁)
// -----------------------------------------------------------------------------
// Monitor Enter/Exit
// The interpreter and compiler assembly code tries to lock using the fast path
// of this algorithm. Make sure to update that code if the following function is
// changed. The implementation is extremely sensitive to race condition. Be careful.
void ObjectSynchronizer::enter(Handle obj, BasicLock* lock, JavaThread* current) {
//是否开启偏向锁
if (obj->klass()->is_value_based()) {
handle_sync_on_value_based_class(obj, current);
}
//获取对象的markword
markWord mark = obj->mark();
//如果
if (mark.is_neutral()) {
// Anticipate successful CAS -- the ST of the displaced mark must
// be visible <= the ST performed by the CAS.
lock->set_displaced_header(mark);
if (mark == obj()->cas_set_mark(markWord::from_pointer(lock), mark)) {
return;
}
// Fall through to inflate() ...
} else if (mark.has_locker() &&
current->is_lock_owned((address)mark.locker())) {
assert(lock != mark.locker(), "must not re-lock the same lock");
assert(lock != (BasicLock*)obj->mark().value(), "don't relock with same BasicLock");
lock->set_displaced_header(markWord::from_pointer(NULL));
return;
}
// The object header will never be displaced to this lock,
// so it does not matter what the value is, except that it
// must be non-zero to avoid looking like a re-entrant lock,
// and must not look locked either.
lock->set_displaced_header(markWord::unused_mark());
// An async deflation can race after the inflate() call and before
// enter() can make the ObjectMonitor busy. enter() returns false if
// we have lost the race to async deflation and we simply try again.
while (true) {
ObjectMonitor* monitor = inflate(current, obj(), inflate_cause_monitor_enter);
if (monitor->enter(current)) {
return;
}
}
}
总结
今天主要对Java中synchronized关键字的底层原理的分析,对于jdk的对于它的锁优化原理, 由无锁-->偏向锁-->轻量级锁(CAS)-->重量级锁(OjectMonitor)有一个清晰的认识。