Let's say I have the following class in Java:
class Record {
String name;
double count;
long repeat;
public Record(String name){
this.name = name;
}
public synchronized void update(Record other){
this.count = (other.count * other.repeat + this.count * this.repeat)/(other.repeat + this.repeat);
this.repeat = this.repeat + other.repeat;
}
Now I have a map of such records ConcurrentHashMap<String, Record> recordConcurrentHashMap;
and I want to create a thread-safe correct update function.
Currently I have done this:
static ConcurrentHashMap<String,Record> recordConcurrentHashMap;
public static void updateRecords(Record other){
Record record = recordConcurrentHashMap.computeIfAbsent(other.name, Record::new);
record.update(other);
}
I am having to keep the update
function synchronized to achieve correctness.
Can I do this without synchronized
using LongAdder
or LongAccumulator
?
I tried using those, but couldn't figure out how to achieve the complex calculation with them.