No runtime warnings for synchronization on value-based classes

David Holmes david.holmes at oracle.com
Wed Feb 24 05:55:38 UTC 2021


Hi,

On 24/02/2021 3:30 pm, Zheka Kozlov wrote:
> Hi!
> 
> // Main.java
> public class Main {
>      public static void main(String[] args) {
>          f(0D);
>      }
> 
>      private static void f(Double value) {
>          synchronized (value) {
>              System.out.println(value);
>          }
>      }
> }
> 
>> java --version
> openjdk 16 2021-03-16
> OpenJDK Runtime Environment (build 16+36-2231)
> OpenJDK 64-Bit Server VM (build 16+36-2231, mixed mode, sharing)
> 
>> javac Main.java
> Main.java:7: warning: [synchronization] attempt to synchronize on an
> instance of a value-based class
>          synchronized (value) {
>          ^
> 1 warning
> 
>> java Main
> 0.0
> 
> According to JEP 390 [1], there should be a runtime warning. But there is
> none. Or did I miss something?

You need to enable it:

HotSpot implements runtime detection of monitorenter occurring on a 
value-based class instance. The command-line option 
-XX:DiagnoseSyncOnValueBasedClasses=1 will treat the operation as a 
fatal error. The command-line option 
-XX:DiagnoseSyncOnValueBasedClasses=2 will turn on logging, both via the 
console and via JDK Flight Recorder events.

---

Cheers,
David

> [1] https://openjdk.java.net/jeps/390
> 


More information about the jdk-dev mailing list