com.zc.my.A object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) 01 00 00 00 (00000001 00000000 00000000 00000000) (1)
4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0)
8 4 (object header) 43 c1 00 f8 (01000011 11000001 00000000 11111000) (-134168253)
12 1 boolean A.flag false
13 3 (loss due to the next object alignment)
Instance size: 16 bytes
Space losses: 0 bytes internal + 3 bytes external = 3 bytes total
偏向锁
publicstaticvoidmain(String[] args)throws InterruptedException {
Thread.sleep(5000);
A a =newA();
System.out.println(ClassLayout.parseInstance(a).toPrintable());}
com.zc.my.A object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) 05 00 00 00 (00000101 00000000 00000000 00000000) (5)
4 4 (object header) 00 00 00 00 (00000000 00000000 00000000 00000000) (0)
8 4 (object header) e4 1c 01 f8 (11100100 00011100 00000001 11111000) (-134144796)
12 1 boolean A.flag false
13 3 (loss due to the next object alignment)
Instance size: 16 bytes
Space losses: 0 bytes internal + 3 bytes external = 3 bytes total
轻量级锁
publicstaticvoidmain(String[] args)throws Exception {
Thread.sleep(5000);
A a =newA();
Thread thread1=newThread(){@Overridepublicvoidrun(){synchronized(a){
System.out.println("thread1 locking");
out.println(ClassLayout.parseInstance(a).toPrintable());//偏向锁}}};
thread1.start();
thread1.join();
Thread.sleep(10000);synchronized(a){
out.println("main locking");
out.println(ClassLayout.parseInstance(a).toPrintable());//轻量锁}}
com.zc.my.A object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) 05 c8 86 5a (00000101 11001000 10000110 01011010) (1518782469)
4 4 (object header) ea 7f 00 00 (11101010 01111111 00000000 00000000) (32746)
8 4 (object header) 3c 1e 01 f8 (00111100 00011110 00000001 11111000) (-134144452)
12 1 boolean A.flag false
13 3 (loss due to the next object alignment)
Instance size: 16 bytes
Space losses: 0 bytes internal + 3 bytes external = 3 bytes total
main locking
com.zc.my.A object internals:
OFFSET SIZE TYPE DESCRIPTION VALUE
0 4 (object header) d8 e8 39 01 (11011000 11101000 00111001 00000001) (20572376)
4 4 (object header) 00 70 00 00 (00000000 01110000 00000000 00000000) (28672)
8 4 (object header) 3c 1e 01 f8 (00111100 00011110 00000001 11111000) (-134144452)
12 1 boolean A.flag false
13 3 (loss due to the next object alignment)
Instance size: 16 bytes
Space losses: 0 bytes internal + 3 bytes external = 3 bytes total
Process finished with exit code 0