Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple lock objects necessary?

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.

like image 227
Stuart Blackler Avatar asked Feb 11 '13 14:02

Stuart Blackler


People also ask

Can we create more than one lock object for a table?

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.

Do I need to lock reads?

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.

Why do we use lock objects?

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.

What do you mean by locking of objects?

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.


1 Answers

Yes, it is correct. It avoids needlessly locking one list just because the other list is being worked on.

like image 90
Matthew Watson Avatar answered Sep 29 '22 05:09

Matthew Watson