-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBiasedLock.java
29 lines (23 loc) · 1003 Bytes
/
BiasedLock.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
package org.mtokarski.object.header.lock;
import org.mtokarski.object.header.HeaderUtil;
/**
* Setting -XX:BiasedLockingStartupDelay=0 will enable biased locking
*/
public class BiasedLock {
public static void main(String... args) throws InterruptedException {
Object o = new Object();
System.out.println("Initially object is: " + HeaderUtil.extractLockState(o));
synchronized (o) {
System.out.println("When used to lock, object is: " + HeaderUtil.extractLockState(o));
}
System.out.println("After synchronisation object is: " + HeaderUtil.extractLockState(o));
Thread thread = new Thread(() -> {
synchronized (o) {
System.out.println("When synchronised in a different thread object is: " + HeaderUtil.extractLockState(o));
}
});
thread.start();
thread.join();
System.out.println("After synchronisation object is: " + HeaderUtil.extractLockState(o));
}
}