-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeavyLock.java
40 lines (34 loc) · 1.56 KB
/
HeavyLock.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package org.mtokarski.object.header.lock;
import org.mtokarski.object.header.HeaderUtil;
public class HeavyLock {
public static void main(String[] args) throws InterruptedException {
Object o = new Object();
o.hashCode();
System.out.println("At the beginning object is in state: " + HeaderUtil.extractLockState(o));
long objectHeader = HeaderUtil.getObjectHeader(o);
System.out.println("Object's header is: " + Long.toBinaryString(objectHeader));
System.out.println();
Thread thread = new Thread(() -> {
synchronized (o) {
System.out.println("Finally inside synchronised in thread");
}
});
synchronized (o) {
thread.start();
Thread.sleep(1000);
System.out.println("In synchronised block object is in state: " + HeaderUtil.extractLockState(o));
System.out.println("Pointer to monitor is: " + Long.toBinaryString(HeaderUtil.getObjectHeader(o)));
long displacedObjectHeader = HeaderUtil.getDisplacedHeader(o);
System.out.println("Displaced header: " + Long.toBinaryString(displacedObjectHeader));
if (objectHeader == displacedObjectHeader) {
System.out.println("Displaced header matches original one");
}
System.out.println();
}
thread.join();
//monitor is removed after gc
System.gc();
System.out.println();
System.out.println("After synchronised block object is: " + HeaderUtil.extractLockState(o));
}
}