Question

From the release notes, checkstyle removed the Double-Checked locking check.

I'm having a hard time understanding why. They replied this not only in the release notes but also in the issue tracker:

Removed the DoubleCheckedLocking check, as in Java 5 (and beyond), using the volatile keyword addresses the issue.

I'm assuming that, if checkstyle is removing this warning, it's because it is no longer helpful. That is, either the error won't happen anymore or another warning does the job. But

I can't see why such error won't happen anymore in Java 5, or how it is complemented by another warning. Could someone care to explain?

EDIT: I understand how adding the volatile keyword solves the issue. My concern is: isn't this warning still worth it somehow? I'm thinking on the case when the programmer uses the aforementioned locking pattern, but forgets to declare the variable volatile. Shouldn't checkstyle still warn about it?

Was it helpful?

Solution

The description pretty much explains the decision. As of Java 1.5 you can use volatile instance variable. It will correctly handle memory visibility issues and using double checked locking is no longer a bug.

It doesn't mean that using volatile is the solution. But in 1.5 Java Memory Model was redefined, making volatile sufficient.

See also

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top