Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double-Checked Locking disappeared from checkstyle - why?

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?

like image 567
Thiago Avatar asked Feb 07 '26 01:02

Thiago


1 Answers

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

  • The "Double-Checked Locking is Broken" Declaration
like image 112
Tomasz Nurkiewicz Avatar answered Feb 08 '26 13:02

Tomasz Nurkiewicz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!