You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Thanks for the report. However, I think we should not soften the rule. The rule should be as simple as "AvoidUsingVolatile" - no exceptions.
The rule predates my involvement with the project, but I think the underlying idea is something like this: "Writing concurrency programs that work correctly is extremely difficult. Using volatile - which is a low-level concurrency tool - should be avoided by any developer, as you most likely make errors no matter what. If you think you are an expert, then you can suppress or disable the rule and use volatile - then we assume, you know what you are doing. But usually, you should not use any low-level concurrency construct yourself."
This aligns with the recommendation from Effective Java: "Prefer concurrency utilities to wait and notify". I consider volatile also a low-level concurrency util and the high-level concurrency utils in java.util.concurrent should be used.
While the JDK experts are also just humans and may make mistakes, concurrency issues in such central JDK classes should be noticed quite fast and fixed. So you can in general assume, all bugs are solved already and you are using a well tested high-level concurrency util.
The general advice here is simple: Use well-known matured implementations and not try yourself to create your own concurrent hashmap etc. These well-known matured implementations are used by many, many more users and bugs probably have shown already and have been fixed. If you create your own, then you are the only user of it and it's much more likely that it contains concurrency bugs, that will only show in specific circumstances.
Your finding about double/long values can be added to the rule documentation. Also note, that just using volatile would only solve the problem of visibility, but not atomicity (e.g. if the change of the variable depends on the current value - so you need to read and write atomically). There is a simple high-level concurrency util that should be used instead at least of long: AtomicLong. For double, there is a bit work to do (as there is no standard AtomicDouble).
But if you have anyway more than one variable, that needs to be consistently updated, then you anyway need some sort of synchronization.
IMHO we should improve the documentation of the rule and try to explain better, why this rule exists and what it tries to achieve/prevent and what the alternatives are.
changed the title [-][java] AvoidUsingVolatile: Do not report long and double[/-][+][java] AvoidUsingVolatile: False positive for long and double[/+]on Jan 16, 2026
changed the title [-][java] AvoidUsingVolatile: False positive for long and double[/-][+][java] AvoidUsingVolatile: Update documentation[/+]on Aug 21, 2026
Activity
adangel commentedon Dec 18, 2025
Thanks for the report. However, I think we should not soften the rule. The rule should be as simple as "AvoidUsingVolatile" - no exceptions.
The rule predates my involvement with the project, but I think the underlying idea is something like this: "Writing concurrency programs that work correctly is extremely difficult. Using volatile - which is a low-level concurrency tool - should be avoided by any developer, as you most likely make errors no matter what. If you think you are an expert, then you can suppress or disable the rule and use volatile - then we assume, you know what you are doing. But usually, you should not use any low-level concurrency construct yourself."
This aligns with the recommendation from Effective Java: "Prefer concurrency utilities to
waitandnotify". I considervolatilealso a low-level concurrency util and the high-level concurrency utils injava.util.concurrentshould be used.While the JDK experts are also just humans and may make mistakes, concurrency issues in such central JDK classes should be noticed quite fast and fixed. So you can in general assume, all bugs are solved already and you are using a well tested high-level concurrency util.
The general advice here is simple: Use well-known matured implementations and not try yourself to create your own concurrent hashmap etc. These well-known matured implementations are used by many, many more users and bugs probably have shown already and have been fixed. If you create your own, then you are the only user of it and it's much more likely that it contains concurrency bugs, that will only show in specific circumstances.
Your finding about double/long values can be added to the rule documentation. Also note, that just using volatile would only solve the problem of visibility, but not atomicity (e.g. if the change of the variable depends on the current value - so you need to read and write atomically). There is a simple high-level concurrency util that should be used instead at least of long: AtomicLong. For double, there is a bit work to do (as there is no standard AtomicDouble).
But if you have anyway more than one variable, that needs to be consistently updated, then you anyway need some sort of synchronization.
IMHO we should improve the documentation of the rule and try to explain better, why this rule exists and what it tries to achieve/prevent and what the alternatives are.
mrclmh commentedon Dec 26, 2025
I see. I will try to rework the PR as a documentation change.
[-][java] AvoidUsingVolatile: Do not report long and double[/-][+][java] AvoidUsingVolatile: False positive for long and double[/+][-][java] AvoidUsingVolatile: False positive for long and double[/-][+][java] AvoidUsingVolatile: Update documentation[/+][java] Fix #6297: Update AvoidUsingVolatile description (#6992)