Given the following class:
class x
{
Object lockOne = new Object();
Object lockTwo = new Object();
List<Something> listOne = new List<Something>();
List<Something> listTwo = new List<Something>();
void MethodOne()
{
lock(lockOne)
{
// some operation on listOne
}
}
void MethodTwo()
{
lock(lockTwo)
{
// some operation on listTwo
}
}
}
Is it correct to use two locking objects assuming that MethodOne()
and MethodTwo()
can be called from different threads concurrently noting that listOne
and listTwo
are not related in anyway. The only operations involved in the locks are those specified in the comments above.
Yes. Remember that you may indicate a DDIC structure for a lock object if you want. The lock system just needs a structure with fields to define locks, it doesn't worry of anything else.
depends on how you use and read it. if your read is atomic (i.e, won't be interrupted by write) and the read thread does not have dependency with the write threads, then you maybe able to skip read lock. But if your 'read' operation takes some time and takes heavy object interation, then you should lock it for read.
Lock Object is a feature offered by ABAP Dictionary that is used to synchronize access to the same data by more than one program. Data records are accessed with the help of specific programs. Lock objects are used in SAP to avoid the inconsistency when data is inserted into or changed in the database.
An object-level lock is a mechanism when we want to synchronize a non-static method or non-static code block such that only one thread will be able to execute the code block on a given instance of the class. If a thread wants to execute a synchronized method on the given object.
Yes, it is correct. It avoids needlessly locking one list just because the other list is being worked on.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With