Close

Java - java.lang.System Examples

Java 

Using identityHashCode() method

import java.util.ArrayList;
import java.util.List;

public class LazyInitBlockDemo {
private volatile List<String> list;

public static void main (String[] args) throws InterruptedException {
LazyInitBlockDemo obj = new LazyInitBlockDemo();

Thread thread1 = new Thread(() -> {
System.out.println("thread1 : " + System.identityHashCode(obj.getList()));
});
Thread thread2 = new Thread(() -> {
System.out.println("thread2 : " + System.identityHashCode(obj.getList()));
});

thread1.start();
thread2.start();
}

private List<String> getList () {
if (list == null) {
synchronized (this) {
if(list == null) {
list = new ArrayList<>();
}
}
}
return list;
}
}
Original Post




See Also