17. Multi-threading
public UnsafeValue {
private int max;
public void update(int value){
if(max < value)
max = value;
}
public int get(){
return max;
}
}
http://blogs.sun.com/javaev/entry/ java _2_ _
20. Multi-threading
1
public UnsafeValue {
private int max;
public synchronized void update(int value){
if(max < value)
max = value;
}
public synchronized int get(){
return max;
}
}
21. Multi-threading
2
public UnsafeValue {
private volatile int max;
public synchronized void update(int value){
if(max < value)
max = value;
}
public int get(){
return max;
}
}
22. Multi-threading
public UnsafeValue {
private AtomicInteger max;
public void update(int value){
do{
int old = max.get();
if(old >= value) break;
} while(!max.compareAndSet(old, value));
}
public int get(){
return max.get();
}
}
false