From d328ec35c18df71fdeb087d4dd9e19c1ac96387d Mon Sep 17 00:00:00 2001
From: Reinier Zwitserloot $lock
and $LOCK
fields will of course not be generated if you already created them yourself. You can also choose to lock on another field, by specifying it as parameter to the @Synchronized
annotation. In this usage variant, the fields will not be created automatically, and you must explicitly create them yourself, or an error will be emitted.
Locking on this
or your own class object can have unfortunate side-effects, as other code not under your control can lock on these objects as well, which can cause race conditions and other nasty threading-related bugs.
+
+ If you would prefer java.util.concurrent.locks
style locks (recommended if you're using virtual threads), have a look at @Locked
.