Instruct Nirmaana 24-Smart and Lean Construction Through Technology.pdf
Java volatile
1.
2.
3.
4. Double checked locking
public class Keeper {
private Data data = null;
public Data getData() {
if(data == null) {
synchronized(this) {
if(data == null) {
data = new Data();
}
}
}
return data;
}
}
14. public class SomePopularObject {
public volatile long usefulVal;
public volatile long t1, t2, t3, t4, t5, t6, t7 = 1L;
public long preventOptimisation(){
return t1 + t2 + t3 + t4 + t5 + t6 + t7;
}
}
15. Java 8 @Contended
public class SomePopularObject {
@Contended
public volatile long usefulVal;
public volatile long anotherVal;
}
17. – Herb Sutter, chair of the ISO C++ standards committee, Microsoft.
"Everybody who learns concurrency thinks they
understand it, ends up finding mysterious races they
thought weren’t possible, and discovers that they
didn’t actually understand it yet after all."