Is ConcurrentHashMap's computeIfAbsent thread safe, when nested?
up vote
1
down vote
favorite
I am new to using Map's computeIfAbsent method. I did find (per Java docs) that the entire method invocation is performed atomically. I would like to know (rather confirm) if the following code snippet (inside the method) would be executed atomically.
ConcurrentMap<RunMode, Map<LocalDate, Map<Integer, Set<DomainObject>>>> myCache = new ConcurrentHashmap<>();
public void addToCache (RunMode runMode, LocalDate bizDate, DomainObject bean) {
Set<DomainObject> domainObjSet = myCache.computeIfAbsent(runMode, runModeMap-> new ConcurrentHashMap<>())
.computeIfAbsent(bizDate, bizDateMap-> new ConcurrentHashMap<>())
.computeIfAbsent(bean.getId(), domainSet-> Collections.synchronizedSet(new HashSet<>()).add(bean));
}
java concurrency
add a comment |
up vote
1
down vote
favorite
I am new to using Map's computeIfAbsent method. I did find (per Java docs) that the entire method invocation is performed atomically. I would like to know (rather confirm) if the following code snippet (inside the method) would be executed atomically.
ConcurrentMap<RunMode, Map<LocalDate, Map<Integer, Set<DomainObject>>>> myCache = new ConcurrentHashmap<>();
public void addToCache (RunMode runMode, LocalDate bizDate, DomainObject bean) {
Set<DomainObject> domainObjSet = myCache.computeIfAbsent(runMode, runModeMap-> new ConcurrentHashMap<>())
.computeIfAbsent(bizDate, bizDateMap-> new ConcurrentHashMap<>())
.computeIfAbsent(bean.getId(), domainSet-> Collections.synchronizedSet(new HashSet<>()).add(bean));
}
java concurrency
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am new to using Map's computeIfAbsent method. I did find (per Java docs) that the entire method invocation is performed atomically. I would like to know (rather confirm) if the following code snippet (inside the method) would be executed atomically.
ConcurrentMap<RunMode, Map<LocalDate, Map<Integer, Set<DomainObject>>>> myCache = new ConcurrentHashmap<>();
public void addToCache (RunMode runMode, LocalDate bizDate, DomainObject bean) {
Set<DomainObject> domainObjSet = myCache.computeIfAbsent(runMode, runModeMap-> new ConcurrentHashMap<>())
.computeIfAbsent(bizDate, bizDateMap-> new ConcurrentHashMap<>())
.computeIfAbsent(bean.getId(), domainSet-> Collections.synchronizedSet(new HashSet<>()).add(bean));
}
java concurrency
I am new to using Map's computeIfAbsent method. I did find (per Java docs) that the entire method invocation is performed atomically. I would like to know (rather confirm) if the following code snippet (inside the method) would be executed atomically.
ConcurrentMap<RunMode, Map<LocalDate, Map<Integer, Set<DomainObject>>>> myCache = new ConcurrentHashmap<>();
public void addToCache (RunMode runMode, LocalDate bizDate, DomainObject bean) {
Set<DomainObject> domainObjSet = myCache.computeIfAbsent(runMode, runModeMap-> new ConcurrentHashMap<>())
.computeIfAbsent(bizDate, bizDateMap-> new ConcurrentHashMap<>())
.computeIfAbsent(bean.getId(), domainSet-> Collections.synchronizedSet(new HashSet<>()).add(bean));
}
java concurrency
java concurrency
asked Nov 9 at 17:05
Ram
62
62
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Depends what you mean by atomicity in here.
It is possible that another threads will get a Map from myCache which wont see entry for bizDate.
If you want to have fully populated Map from myCache before any thread will get it then you need to create and populate it inside myCache.computeIfAbsent.
thanks for responding. My primary objective is that all the domain objects (beans) must be added to the map, when multiple threads invoke this method. The bizDate & runMode are mostly constant.
– Ram
Nov 10 at 22:07
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Depends what you mean by atomicity in here.
It is possible that another threads will get a Map from myCache which wont see entry for bizDate.
If you want to have fully populated Map from myCache before any thread will get it then you need to create and populate it inside myCache.computeIfAbsent.
thanks for responding. My primary objective is that all the domain objects (beans) must be added to the map, when multiple threads invoke this method. The bizDate & runMode are mostly constant.
– Ram
Nov 10 at 22:07
add a comment |
up vote
0
down vote
Depends what you mean by atomicity in here.
It is possible that another threads will get a Map from myCache which wont see entry for bizDate.
If you want to have fully populated Map from myCache before any thread will get it then you need to create and populate it inside myCache.computeIfAbsent.
thanks for responding. My primary objective is that all the domain objects (beans) must be added to the map, when multiple threads invoke this method. The bizDate & runMode are mostly constant.
– Ram
Nov 10 at 22:07
add a comment |
up vote
0
down vote
up vote
0
down vote
Depends what you mean by atomicity in here.
It is possible that another threads will get a Map from myCache which wont see entry for bizDate.
If you want to have fully populated Map from myCache before any thread will get it then you need to create and populate it inside myCache.computeIfAbsent.
Depends what you mean by atomicity in here.
It is possible that another threads will get a Map from myCache which wont see entry for bizDate.
If you want to have fully populated Map from myCache before any thread will get it then you need to create and populate it inside myCache.computeIfAbsent.
answered Nov 9 at 17:29
tsolakp
4,46611219
4,46611219
thanks for responding. My primary objective is that all the domain objects (beans) must be added to the map, when multiple threads invoke this method. The bizDate & runMode are mostly constant.
– Ram
Nov 10 at 22:07
add a comment |
thanks for responding. My primary objective is that all the domain objects (beans) must be added to the map, when multiple threads invoke this method. The bizDate & runMode are mostly constant.
– Ram
Nov 10 at 22:07
thanks for responding. My primary objective is that all the domain objects (beans) must be added to the map, when multiple threads invoke this method. The bizDate & runMode are mostly constant.
– Ram
Nov 10 at 22:07
thanks for responding. My primary objective is that all the domain objects (beans) must be added to the map, when multiple threads invoke this method. The bizDate & runMode are mostly constant.
– Ram
Nov 10 at 22:07
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53230309%2fis-concurrenthashmaps-computeifabsent-thread-safe-when-nested%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown