patternjavaMinor
Using CountDownLatch for blocking the reads if writes are happening
Viewed 0 times
happeningthewritesareblockingreadsforusingcountdownlatch
Problem
I am trying to implement lock by which I want to avoid reads from happening whenever I am doing a write on my three maps.
Requirements:
As I have three Maps -
It should be consistent, either it should return old values or it should return new values after/while updating the maps. In my case, updating of maps will happen once in three months or four months.
And from the main application thread, reads will be happening on the getter of those three maps at a rate of
Below is my
```
public class ClientData {
public static class Mappings {
public final Map> primary;
public final Map> secondary;
public final Map> tertiary;
public Mappings(
Map> primary,
Map> secondary,
Map> tertiary
) {
this.primary = primary;
this.secondary = secondary;
this.tertiary = tertiary;
}
}
private static final AtomicReference mappings = new AtomicReference<>();
private static final CountDownLatch hasBeenInitialized = new CountDownLatch(1);
public static Mappings g
Requirements:
- Reads block until all three maps have been set for the first time.
- Now second time, If I am updating the maps, I can still return all the three old maps value(before the updates are done on all three maps) or it should block and return me all the new three maps value whenever the updates are done on all the three maps.
As I have three Maps -
primary , secondary and tertiary so it should return either all the new values of three updated maps or it should return all the old values of the map. Basically, while updating I don't want to return primary having old values, secondary having having new values, and tertiary with new values.It should be consistent, either it should return old values or it should return new values after/while updating the maps. In my case, updating of maps will happen once in three months or four months.
And from the main application thread, reads will be happening on the getter of those three maps at a rate of
1000 requests per second. So the get call on the three maps are really performance critical. I was thinking to use ReentrantLock or ReentrantReadWriteLock but got stick to CountDownLatch because of simplicity-Below is my
ClientData class in which I am using CountDownLatch -```
public class ClientData {
public static class Mappings {
public final Map> primary;
public final Map> secondary;
public final Map> tertiary;
public Mappings(
Map> primary,
Map> secondary,
Map> tertiary
) {
this.primary = primary;
this.secondary = secondary;
this.tertiary = tertiary;
}
}
private static final AtomicReference mappings = new AtomicReference<>();
private static final CountDownLatch hasBeenInitialized = new CountDownLatch(1);
public static Mappings g
Solution
Going through some smaller items in your code. Your
On the
Restricting these permissions will make your application better defined.
The remaining concern with your code is whether the Map instances themselves are Mutable (well, they are mutable, but should they be?). I would recommend making them readonly using Collections.unmodifiableMap() which will prevent the actual Map content from becoming corrupted by some thread polluting the data.
Mappings class is public, which is fine, but it should also be final. There is no reason for people to extend your class. The constructor should be private, and all the Map fields should be private as well, but you should have getters for those maps... getPrimary(), getSecondary(), etc.On the
ClientData class, the method setMappings(Mappings...) content should be merged in to the setMappings(Map ..., Map.. Map) method, and that method should not be public either, assuming the background thread is defined in the same package as you.Restricting these permissions will make your application better defined.
The remaining concern with your code is whether the Map instances themselves are Mutable (well, they are mutable, but should they be?). I would recommend making them readonly using Collections.unmodifiableMap() which will prevent the actual Map content from becoming corrupted by some thread polluting the data.
Context
StackExchange Code Review Q#48526, answer score: 4
Revisions (0)
No revisions yet.