Summary
Add a new lint option, synchronization
, to javac
to warn about usages of the synchronized
statement with an operand that is an instance of a value-based class.
Problem
The Valhalla Project is pursuing a significant enhancement to the Java programming model in the form of primitive classes, that offer various performance optimization opportunities. Several classes in the Java Platform that have been informally designated as value-based classes in the API specifications are candidates for being migrated to primitive classes in a future release.
Synchronization on instances of value-based classes is discouraged, because the classes' factories do not promise unique ownership of an object identity. After migrating to primitive classes, attempts to synchronize on instances of these classes will produce exceptions, since primitive class instances have no monitors associated with them that can be locked.
Solution
Enhance javac
to detect synchronized
statements that have operands that are instances of value-based classes and warn programmers that these synchronization operations are discouraged.
These compile-time checks will be complemented by dynamic checks in the JVM, via JDK-8252182.
Specification
The new javac
lint category synchronization
is turned on by default and produces the following diagnostic message:
warning: [synchronization] attempt to synchronize on an instance of a value-based class.
- csr of
-
JDK-8252183 [JEP 390] Add 'lint' warning for @ValueBased classes
- Resolved