Locking on objects

I need clarification on what object gets locked when you say
synchronized( lockObj )
// Do something
Assume that both the class that contains the above snippet as well as the class of lockObj have synchronized methods. Now, when a thread enters the critical section, does the jvm prevent other threads from accessing the synchronized methods of this object or of lockObj (or both)?
I have seen in Item 52 of Effective Java that Bloch shows an example of locking the backing Hashtable when obtaining a key Enumeration. This implies that the lockObj gets 'locked'. But in the same item, at the end a code snippet is shown where a private 'lock' object is used to block access to synchronized methods of the object having the private 'lock' object. I am confused!

Given you have:
public class MyClass1
    private MySemaphore semaphore;
    public synchronized void mehotd1()
    public void method2()
        synchronized(semaphore)
    public void synchronized void mehotd3()
public class MySemaphore
    public synchronized void sema1()
    public void synchronized void sema2()
}Now let say you have 3 threads, named thread1, thread2 and thread3.
If thread1 execute method1(), a lock will be set on the MyClass object and while the lock is on, if thread2 try to access either method1 or method3, it will go in a waiting queue until thread1 finish to execute method1. However thread2 can still access method2 as it don't lock on the MyClass object, but on the MySemaphore one. While thread2 would be executing method2 code, thread1 and thread3 would not be able to execute sema1() or sema2() as those requires a lock on the MySemaphore object, a lock that is owned by thread2.
This said synchronization can cause dead lock, look at this code:
public class MyClass
    public synchronized void foo(Object semaphore)
        // Some code
        synchronized(semaphore)
            // Some more code
public class MyThread extends Thread
    private MyClass object;
    private MyClass semaphore;
    public MyThread(MyClass object, MyClass semaphore)
        this.object = object;
        this.semaphore = semaphore;
    public static void main(String[] args)
        MyClass object1 = new MyClass();
        MyClass object2 = new MyClass();
        MyThread thread1 = new MyThread(object1, object2);
        MyThread thread2 = new MyThread(object2, object1);
        thread1.start();
        thread2.start();
    public void run()
        object.foo(semaphore);
}This code may lock the JVM as what may happen is:
thread1 call object1.foo(object2) and acquires the lock on object1 (foo is synchronized)
thread2 call object2.foo(object1) and acquires the lock on object2
thread1 execute "some code" of foo method and reach the synchronized(object2) part so go in thw waiting queue as the lock on object2 is held by thread2
thread2 execute "some code" of foo method and reach the synchronized(object1) part so go in thw waiting queue as the lock on object1 is held by thread1
Everything is in the waiting queue and cannot resolve.
Basically synchronizing on more than one object can be dangerous. Sometimes it is needed but it requires the programmer to think twice about it.
Regards

Similar Messages

  • Error "Problem occured locking WS object" while activating Proxy

    Hi All,
    I have move an Inbout proxy to a different Client(QA system) and in QA system that proxy shows incative.
    When i tried to activate that proxy/service in Sproxy, getting error "Problem occured locking WS object".
    Proxy was active in development system and was working fine without any error.
    Please provide some help, what need to be done to fix this issue
    Thanks
    Harshit

    Hi Harshit,
    Try with the following steps in sequence:
    1. Re-genarate the proxy in Development - Create a New Transport Reuest.
    2. Transport the Proxy from PI Dev to PI OA
    3. Confirm the Proxy in PI QA and then, transport the re-generated Request from ECC Dev to ECC QA.
    Regards,
    Pranav.

  • Lock Selected objects

    Hi
    Any one please help me to use lock option in javascript
    i want to lock selected objects
    Thank
    APPU

    surendarappu wrote:
    help me to hide instead of lock
    To modify/change the code provided by Muppet Mark, from locking to hiding, see the following:
    // Change this line...
    // selectList[i].locked = true;
    // To the following...
    selectList[i].hidden = true;
    You could also change the two references of "lockSelection" for the function name and call, to something like "hideSelection" to avoid confusion as well if needed.
    * Was this helpful?

  • FM to find user who locked the object using enqueue...

    Hello,
    How can I find name of user who enqueued the lock object in ABAP ?
    Regards,
    Jainam.

    Hi Jainam,
    See the SAP documentation, e.g. [FAQ - Lock concepts|http://help.sap.com/saphelp_NW04/helpdata/en/cb/168237d30d974be10000009b38f8cf/content.htm]:
    How can I find out who is currently holding the ungranted lock? In other words, how can check the program after an ENQUEUE to determine which use is currently holding the lock so that I can let him or her know?                                 
    This graphic is explained in the accompanying text Answer
    When the ENQUEUE_... function module is returned, the name of the lock owner is listed in SY-MSGV1.
    If you don't want to attempt to lock an object and just check who might own a lock use function module ENQUEUE_READ. Lots of comments in the forum...
    Cheers, harald

  • Locking an object problem

    We are customizing our Plumtree Portal 5.0.2 to all the gadgets developers to send an HTTP header called "Refresh-all","Gadget Name". When this header is sent the portal should refresh the gadget provided in the header to flush it's content. The only problem that we are facing now is storing the changes made to the Gadget Admin Settings
    here's the code
    IPTObjectManager objectManager = (IPTObjectManager) ptSession.GetGadgets();
    IPTGadget gadgetToRefresh = (IPTGadget)objectManager.Open(portletID,false);
    IPTServerContext serverContext = (IPTServerContext)gadgetToRefresh.GetInterfaces("IPTServerContext");
    serverContext.LockObject();
    serverContext.Store();
    serverContext.UnlockObject();
    When the logged in user is not an admin this gives an error and PTSpy reported that the user doesn't have enough priviliges to lock the object. the ptSession is the current user session retrieved by the function GetUserSession()
    Any proposed work around or any way to get an admin session will be great
    TIA

    We are customizing our Plumtree Portal 5.0.2 to all the gadgets developers to send an HTTP header called "Refresh-all","Gadget Name". When this header is sent the portal should refresh the gadget provided in the header to flush it's content. The only problem that we are facing now is storing the changes made to the Gadget Admin Settings
    here's the code
    IPTObjectManager objectManager = (IPTObjectManager) ptSession.GetGadgets();
    IPTGadget gadgetToRefresh = (IPTGadget)objectManager.Open(portletID,false);
    IPTServerContext serverContext = (IPTServerContext)gadgetToRefresh.GetInterfaces("IPTServerContext");
    serverContext.LockObject();
    serverContext.Store();
    serverContext.UnlockObject();
    When the logged in user is not an admin this gives an error and PTSpy reported that the user doesn't have enough priviliges to lock the object. the ptSession is the current user session retrieved by the function GetUserSession()
    Any proposed work around or any way to get an admin session will be great
    TIA

  • Should we lock the object

    Hi , i have a question , if i want to insert the data Concurrency, what should i do ? whether i should to lock the object,and to read or write?if you know the answer ,tell me,thanks!

    See:
    http://www.oracle.com/technology/documentation/berkeley-db/je/TransactionGettingStarted/index.html

  • Locking of objects between service calls

    Hello,
    Is there any practial way to lock an object (for example a loan) with one service so that it stays locked until another service commits the transaction and releases the lock? The service is a RFC function module that will be called by a web application. My problem is that when I use the standard ENQUEUE function the lock dissappears as soon as the first service is finished.
    Also, if one service would create a lock of an object and another one starts the update task, they probably wouldn't be in the same LUW, would that be a problem? If so, is there a solution?
    Thank you and kind regards,
    Steffen

    Steffen:
    In the locking service use the _SCOPE parameter value of 3 in your enqueue function call. This retains the lock after the first service (locking process) ends. The second service that does the update can then dequeue the lock after the update is processed.
    No problem with setting & freeing locks in different LUW, as the SAP locking mechanism is co-operative (i.e. software controlled) rather than database controlled. Just make sure that you use a standard SAP enqueue function so that other unrelated processes will honor the lock set by your first service.
    Regards,
    D.

  • Index creation error. Can't lock configuration object.

    Hi,
    My Trex it is giving me a lot of trouble. I am not able to create index with the Trex Search and Classification service (the message I get is==> Index could not be created: null)
    The defaulttrace says:
    #1.5 #0017A4F64A4801160000003200001F7000043C109F857E90#1191942000781#com.sap.workflow#sap.com/irj#com.sap.workflow#Guest#0####9fd2cf80767011dc9b4f0017a4f64a48#Thread[ThreadPool.Worker4,5,SAPEngine_Application_Thread[impl:3]_Group]##0#0#Error#1#/Applications/Workflow/General#Plain###TaskSchedulerDeadline.run()#
    #1.5 #0017A4F64A4801160000003300001F7000043C109F859265#1191942000797#com.sap.workflow#sap.com/irj#com.sap.workflow.#Guest#0####9fd2cf80767011dc9b4f0017a4f64a48#Thread[ThreadPool.Worker4,5,SAPEngine_Application_Thread[impl:3]_Group]##0#0#Error##Plain###LockConfigException: Cannot lock configuration object "config://local/com.sap.workflow/scheduler/task" without a valid user ID.
    at com.sapportals.config.fwk.data.ConfigLockManager.lock(ConfigLockManager.java:516)
    at com.sapportals.config.fwk.data.ConfigLockManager.lock(ConfigLockManager.java:265)
    at com.sapportals.config.fwk.data.Configurable.lock(Configurable.java:184)
    at com.sapportals.config.fwk.data.ConfigPlugin.getAndLockConfigurable(ConfigPlugin.java:452)
    at com.sap.workflow.cfg.AbstractConfiguration.setValue(AbstractConfiguration.java:313)
    at com.sap.workflow.es.scheduler.TaskSchedulerDeadline.run(TaskSchedulerDeadline.java:86)
    at com.sapportals.wcm.service.scheduler.SchedulerEntry.run(SchedulerEntry.java:174)
    at com.sapportals.wcm.service.scheduler.crt.PoolWorker.run(PoolWorker.java:108)
    at java.lang.Thread.run(Thread.java:534)
    What means the message "Cannot lock configuration object "config://local/com.sap.workflow/scheduler/task" without a valid user ID."?
    Have I to do something abut configuration, users, permissions??
    The version is 7.00 patch 57
    Operation system is WServer 2003SR2 Enterprise Edition (En)
    I have installed the TREX HTTP server
    In Monitoring I see a green light for http server, index server, name server, queue server
    I am so confused, don't know wich way to turn.
    Any help will be appreciated.
    Thanks in advance.
    Guillermo.

    Hi,
    It was an error caused by permissions.
    By mistake the repository /taxonomies was manipulated by an user and the permissions were changed.
    After it the portal wasn't able to create new folders under that repository.
    Regards.

  • Moving group/layer/artboard with locked child objects

    In CS5, you could move a group/layer/artboard along with all of the child objects, even if some of the objects are locked.  But in CS6 the locked objects remain unmoved.
    Is there a way to change this behaviour?
    TIA

    I don't know about this convwersation I found some interesting things that might suggest that this is intentional.
    For one if you lock an object(s) in a group in CS 6and try to target the group you will only select the unlocked objects in the group  and effects will only be applied to the unlocked objects. But if you have objects and groups locked ( even if the objects are in groups) and you taarget the layer it will only slect the unlocked objects in the layer and or groups and transforms and effects will only be applies the those objects but if you select the layer using the selection area in the layer then eveything on that layer will be selected and transforms and effects will be applied to everything in the layer.
    I think this is intended to work this way but I think the groups should also worrk this way adding power to the layers panel and more meaning to the target feature.

  • Locks on objects

    When are locks on objects in a change request released

    Hai,
    Gothrough the following link,
    http://help.sap.com/saphelp_nw04s/helpdata/en/cf/21eea5446011d189700000e8322d00/content.htm
    How to Create Lock Object :
    Lock objects:
    Lock objects are use in SAP to avoid the inconsistancy at the time of data is being insert/change into database.
    SAP Provide three type of Lock objects.
    Read Lock(Shared Locked)
    protects read access to an object. The read lock allows other transactions read access but not write access to
    the locked area of the table
    Write Lock(exclusive lock)
    protects write access to an object. The write lock allows other transactions neither read nor write access to
    the locked area of the table.
    Enhanced write lock (exclusive lock without cumulating)
    works like a write lock except that the enhanced write lock also protects from further accesses from the
    same transaction.
    You can create a lock on a object of SAP thorugh transaction SE11 and enter any meaningful name start with EZ Example EZTEST_LOCK.
    Use: you can see in almost all transaction when you are open an object in Change mode SAP could not allow to any other user to open the same object in change mode.
    Example: in HR when we are enter a personal number in master data maintainance screen SAP can't allow to any other user to use same personal number for changes.
    Technicaly:
    When you create a lock object System automatically creat two function module.
    1. ENQUEUE_<Lockobject name>. to insert the object in a queue.
    2. DEQUEUE_<Lockobject name>. To remove the object is being queued through above FM.
    You have to use these function module in your program.
    first one is used to lock the table
    second one used to removing lock on the table.
    lock Table
    CALL FUNCTION 'ENQUEUE_E_TABLE'
    EXPORTING
    tabname = table_name
    EXCEPTIONS
    foreign_lock = 1
    system_failure = 2
    OTHERS = 3.
    Unlock Table
    CALL FUNCTION 'DEQUEUE_E_TABLE'
    EXPORTING
    tabname = table_name.
    Hope this will help to you....
    Regards,
    Harish

  • How to lock PI objects at customer?

    Hi,
    apart from locking SWC, ( making objects not modifiable ) is there any other way to lock PI objects permenantly before giving it to customer?
    regards,
    venkat.

    Venkat,
    This is already discussed in this threads:
    XI Authorisation only for  display
    XI Authorisation only for  display
    Security roles needed for IR and ID access
    Please go through them.
    Regards,
    ---Satish

  • How can I lock the object in my dock?

    Members in my family continue to accidently drag objects off the dock on my powerbook by accident.
    How can I "lock the dock"?
    Thanks.

    You can't. This is a real problem when you accidentally drag 300+ files onto an application icon but miss...
    Matt

  • Query about SAP locks /enqueue objects

    I have created an lock object for a bespoke table which has 5 key fields.  For arguments sake let's call them
    WERKS QMGRP QMCOD OTGRP OTEIL.
    The problem I have is the following:
    User 1 has a house profile that can access the following data:
    WERKS QMGRP QMCOD OTGRP OTEIL
    9026  HOUSE CUST
    User 2  has a functional profile which gives them access to the following:
    WERKS QMGRP QMCOD OTGRP OTEIL
    9026  HOUSE CUST 
    9026  HOUSE CUST   CS    C001
    9026  HOUSE CUST   CS    C002
    I am building a front-end to the table to enable the users to change data associated with these keys.  My problem is the following.  If User 2 locks the entries,(s)he has access to first, the user 1 entry will be locked to changes,  which is correct.  If, however, user 1 locks its entry first, <b>all</b> of user 2's entries are locked even though only the first entry is allowed to be accessed by both.  I think this is happening because the SAP lock object does not recognise '   ' '    ' as a separate value to 'CS' 'C001'.  It thinks because 9026 HOUSE CUST '   ' '    ' is locked, then the other entries should be locked because they share 9026 HOUSE CUST from the key, even though there are other values in the key following 9026 HOUSE CUST.
    What I want is if user 1 locks its entry first, then only the entry that matches the key  totally for user 2 is locked, leaving the other 2 entries to which user 2 has access free to be edited.
    Is there a way I can get the SAP lock object to recognise this eitehr by changing the enqueue function module or by the way I cal the enqueue object or not?
    Any help would be appreciated.
    Hope this makes sense
    Larissa

    Hi Larissa,
    As far as I know, You cannot change generated enqueue function module. The other way as i think might work would be to create a new FM and do manipulation (for space to be changed as some other value) before calling this generated Enqueue FM.
    Hope this helps.
    Regards,
    Vicky
    PS: Award points if helpful

  • How to ensure locks on objects are released after one transaction

    Hi,
    While a program is performing successive operations over the same object, there can be a scenario that after the first operation is completed it may still have a lock over the object. This would result that the object is not available for locking to proceed with the second operation. Hence, the entire record processing would end up in errors. So how we can ensure that after the first operation the locks are released ?
    Regards,
    Ratheesh

    Hi Bhaskaran,
    Please note the Implicit locks set by the system will be deleted whenever the system finds a commit or rollback actions either implicitly or explicitly.
    implicit release of locks takes place whenever there is a CALL SCREEN, LEAVE TO SCREEN,
    SUBMIT PROGRAM, LEAVE TO TRANSACTION, CALL FUNCTION etc.
    for more info Please go through the SAP LUW concept
    Regards
    Ramchander Rao.K

  • Locking and Objects

    I have been looking up a lot of information on locking
    shared-scope variables lately, because I dont really do it at all
    and I probably should. My question revolves around something else I
    had posted about a couple of days ago about creating objects in the
    session scope.
    I have user.cfc that holds information such as username.
    etc... I have methods such as getFirstName(), getLastName(), and
    setUser(userID). These variables are references as
    #Session.User.getFirstName()# as expected.
    My question is, since this is a session variable... should it
    be locked before access?

    jeby wrote:
    > My question is, since this is a session variable...
    should it be locked before
    > access?
    >
    Probably not very often. You should not have to do a lock
    when you are
    just reading values. And you would only need to do a lock
    when changing
    a value in your object if you are concerned about race
    conditions.
    Which for session variables are relatively rare.
    If you where dealing with an object stored in the Application
    or Server
    scope then there would be a much greater chance for race
    conditions to
    occur. Thus you would generally be more concerned about
    locking
    modifying code for these types of objects.
    In early versions of ColdFusion there where memory leak
    problems with
    these persisted scopes and the work around was to always lock
    every
    thing. This issue has long been resolved, but the old cannon
    of locking
    all the time still persists.
    But there can be serious performance, scalability and
    throughput issues
    to over locking your application. So the better pratice is to
    only lock
    when there is a reason to lock.

  • Release the lock for objects in ABAP webdynpro

    Hi All,
    I need to release the lock of certain objects during the application close(When the user closes the browser)
    Can I release the lock of the objects other than the method WDDOEXIT method.  If yes please tell me how?
    The reason being WDDOEXIT is not called during the close.
    Thanks,
    Subash M

    >
    Subash Mohanvel wrote:
    > Hi All,
    > I need to release the lock of certain objects during the application close(When the user closes the browser)
    > Can I release the lock of the objects other than the method WDDOEXIT method.  If yes please tell me how?
    >
    > The reason being WDDOEXIT is not called during the close.
    >
    > Thanks,
    > Subash M
    I think the bigger question is to why WDDOEXIT is not called. What you describe is exactly what WDDOEXIT is designed for.  This is the part of the phase model that is fired when the session times out or the browser closes. This should work for just this purpose.

Maybe you are looking for