Package org.opendaylight.genius.utils
Class JvmGlobalLocks
- java.lang.Object
-
- org.opendaylight.genius.utils.JvmGlobalLocks
-
public final class JvmGlobalLocks extends java.lang.Object
Utility class providing JVM-global locks keyed by String. This class is provided to provide an alternative to the following locking invention:String foo; synchronized (foo.intern()) { // ... }
As such this is an extremely bad idea, as it does not make it clear what the locking domain is and what code is actually participating on it. Until we can get proper locking in place, the above should be replaced with:
String foo; final ReentrantLock = JvmGlobalLocks.getLockForString(foo); lock.lock(); try { // ... } finally { lock.unlock(); }
Once all call sites are identified, their use of Strings should be replaced by domain-specific lock identifiers, so that it is obvious which places actually synchronize on a particular identifier.
getLockFor(Identifier)
andgetLockFor(Identifiable)
provide this functionality.Finally, users of this class should look to downgrade from JVM-global to either an explicit locking service, or to fine-grained locks contained within the component instantiation -- thus rendering this class completely unneeded.
- Author:
- Robert Varga
-
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static @NonNull java.util.concurrent.locks.ReentrantLock
getLockFor(org.opendaylight.yangtools.yang.binding.Identifiable<?> resource)
Return a JVM-globalReentrantLock
for a particular name.static @NonNull java.util.concurrent.locks.ReentrantLock
getLockFor(org.opendaylight.yangtools.yang.binding.Identifier<?> identifier)
Return a JVM-globalReentrantLock
for an identifier.static @NonNull java.util.concurrent.locks.ReentrantLock
getLockForString(java.lang.String lockName)
Deprecated.This is provided only for migration purposes until a proper locking scheme is deployed throughout genius and netvirt.
-
-
-
Method Detail
-
getLockForString
@Deprecated public static @NonNull java.util.concurrent.locks.ReentrantLock getLockForString(java.lang.String lockName)
Deprecated.This is provided only for migration purposes until a proper locking scheme is deployed throughout genius and netvirt.Return a JVM-globalReentrantLock
for a particular string.- Parameters:
lockName
- Name of the lock, must not be null- Returns:
- A JVM-global reentrant lock.
- Throws:
java.lang.NullPointerException
- iflockName
is null
-
getLockFor
public static @NonNull java.util.concurrent.locks.ReentrantLock getLockFor(org.opendaylight.yangtools.yang.binding.Identifier<?> identifier)
Return a JVM-globalReentrantLock
for an identifier.- Parameters:
identifier
- Entity identifier, must not be null- Returns:
- A JVM-global reentrant lock.
- Throws:
java.lang.NullPointerException
- ifidentifier
is null
-
getLockFor
public static @NonNull java.util.concurrent.locks.ReentrantLock getLockFor(org.opendaylight.yangtools.yang.binding.Identifiable<?> resource)
Return a JVM-globalReentrantLock
for a particular name.- Parameters:
resource
- anIdentifiable
resource- Returns:
- A JVM-global reentrant lock.
- Throws:
java.lang.NullPointerException
- ifresource
is null
-
-