Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unnecessary synchronisation? #1572

Open
sebbASF opened this issue Dec 5, 2023 · 1 comment
Open

Unnecessary synchronisation? #1572

sebbASF opened this issue Dec 5, 2023 · 1 comment

Comments

@sebbASF
Copy link

sebbASF commented Dec 5, 2023

The synch. here appears to be unnecessary:

synchronized (customPaths) {
searchPath.addAll(customPaths);
}

searchPaths is a ConcurrentHashMap, so does it need synch?
It also does not make sense to me to synch. on the value being added, rather than the target being updated.

@matthiasblaesing
Copy link
Member

That guard looks unnecessary, but this might be problematic:

public static final void addSearchPath(String libraryName, String path) {
List<String> customPaths = searchPaths.get(libraryName);
if (customPaths == null) {
customPaths = Collections.synchronizedList(new ArrayList<String>());
searchPaths.put(libraryName, customPaths);
}
customPaths.add(path);
}

Here is a real race and that could now (JDK8 as baseline) be fixed using ConcurrentHashMap#computeIfAbsent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants