Q about Readwrite lock in concurrency

If I want to implement read-write data through a ReentrantReadwriteLock,
often we will use :
try {
mylock.lock();
activity...
finally{ mylock.unlock();}
but what if the real"activity" is actually done else where. Can I just make two methods SetLock() and ReleaseLock()
void SetLock(){
mylock.lock();}
void ReleaseLock(){
mylock.unlock;}
and let them called when needed

You're getting this compeltely back to front. Somewhere you have to create the lock object, store it in a variable, acquire the lock, call the method, and release the lock. The only correct way to do that is to release the lock in a 'finally' block, which implies that you have to allocate the lock in the same method so you have the lock reference. So what you are looking for is this:
try
  mylock.lock();
  callTheMethodThatDoesTheActivity();
finally
  mylock.unlock();
}BTW this thread is in the wrong forum. The experts on concurrency are concentrated in the Concurrency forum.

Similar Messages

  • Is this producer-consumer scenario safe about dead-lock?

    Hi friends!
    I'm developing an application with a producer-consumer scenario. It produces a dead-lock 10% of times approx I execute it . I think the real reason of the crashing resides on the concurrency, because executing only the following code it gets the "main" waiting blocked during its execution after looping the do{...}while(true) sometimes. I've developed this code:
    public class CubbyHole
        private int nFinishedThreads;
        private int nTotalThreads;
        public CubbyHole(int TotalThreads)
            this.nTotalThreads = nTotalThreads;
            this.nFinishedThreads = 0;
        public synchronized void put()
            this.nFinishedThreads++;
        public synchronized void get()
            while(nFinishedThreads < nTotalThreads)
                try
                    wait();
                catch(InterruptedException ie){}
            notifyAll();
    //import java.util.Random;
    public class Producer implements Runnable
        private CubbyHole container;
        public Producer(CubbyHole container)
            this.container = container;
        public void run()
            System.out.println(Thread.currentThread().getName() + " has begun");
            try{
                Random rand = new Random();
                Thread.sleep(Math.abs(rand.nextInt()*10));
            catch(InterruptedException ie){}
            System.out.println(Thread.currentThread().getName() + " has finished");
            container.put();
    public class Consumer implements Runnable
        private CubbyHole container;
        public Consumer(CubbyHole container)
            this.container = container;      
        public void run()
            System.out.println(Thread.currentThread().getName() + "is waiting for all threads to be finished");
            container.get();
            System.out.println(Thread.currentThread().getName() + "says all threads have finished");
    public class Application
        public static void main(String Args[])
            do{
                int nThreads = 2;
                CubbyHole container = new CubbyHole(nThreads);
                Thread[] ProducersArray = new Thread[nThreads];
                for(int i=0; i<nThreads; i++)
                    ProducersArray=
    new Thread(
    new Producer(container));
    ProducersArray[i].start();
    Thread finalWait = new Thread( new Consumer(container));
    finalWait.start();
    try
    finalWait.join();
    catch(InterruptedException ie){}
    System.out.println("Application has finished correctly");
    while(true);
    Please, execute it and give me your results: is the "main" waiting blocked to you?
    Is this producer-consumer code safe about dead-lock?
    If the answer is no... Why is it waiting blocked?
    Thank you in advance.
    P.D. I'm spanish, so my english is horrible! I'm sorry!

    Thanks ejp:
    Moreover, I suppose nothing happens if I write notifyAll() in both methods, put() and get(). In fact this matters only if the producer overwrites the memory space where data is stored, which is not my case.
    Is this so?

  • About pessimistick lock in Oracle

    Hello,
    I have some questions about pessimistick lock in Oracle:
    I know if i do a SELECT..FOR UPDATE statement, those records are locked, and just me can opperate on them.. but me - WHO? How i'm identified in Oracle? If i do a SELECT..FOR UPDATE select in an A procedure, then i call the update procedure B, how can i know that both calls belongs to the same user? If someone tries to update the same records, how the server knows if that person is allowed to make those updates or not? because everyone uses the same connection string.. isn't it?
    Probably it works if i make a SELECT..FOR UPDATE and i make the effective update in that procedure, but this means that the Business Logic is in that procedure..
    Please correct me if i'm wrong somewhere and clarify me.
    Thanks!

    By default the other session has to wait (no error message):
    From SQL Reference SELECT section: http://download.oracle.com/docs/cd/B28359_01/server.111/b28286/statements_10002.htm#SQLRF01702
    >
    NOWAIT | WAIT
    The NOWAIT and WAIT clauses let you tell the database how to proceed if the SELECT statement attempts to lock a row that is locked by another user.
    Specify NOWAIT to return control to you immediately if a lock exists.
    Specify WAIT to instruct the database to wait integer seconds for the row to become available and then return control to you.
    If you specify neither WAIT nor NOWAIT, then the database waits until the row is available and then returns the results of the SELECT statement.
    >
    Edited by: P. Forstmann on 7 janv. 2010 09:00
    Edited by: P. Forstmann on 7 janv. 2010 09:01

  • My ex had set up a dual apple id, which i didn't know about. deleting locked items.

    does this mean that every timed I synced my device he received all my stuff/ photos and things from the last year? I am concerned.
    Also does it mean that he has a copy on his icloud? I had renamed my computer but his id was still there, so when I added mine it looked like
    it had deleted his but somehow it didn't. I only realised when my contacts book on my mac book had his new gf number in it, which couldn't have gotten
    there any other way. Also everytime I went to open address book in user settings it went to his page and at the top it said 'me'. I tried to change all the setting and stuff but figured it was just easier to set up a new user id and delete his, not sure if that was correct. Anyways I have now set up my new user id, made myself the admin and deleted his user id. however, i had moved my files from his to mine via the shared folder and then deleted the shared folder. Now I am wanting to deleted his folder from my trash but it wont. Its saying that there are 83,000 something files and it is taking an eternity, and or just stops.. I tried to delete the locked files other ways but it says i don't have permission. i contacted him and asked that he delete my files if he has them but he has not responded as yet... can someone please help me to understand how this happened or if it even did is there a way that it did? It seems like a huge breach of privacy and I am not happy about it!!!!!
    any help would be really really appreciated.

    Is this your machine or the company's?  Machines that came with Snow Leopard or earlier installed come with system and software install disks that can be used to reisntall the iApps that came with the machine orignally. 
    If this is your machine and it came with Snow Leopard or earlier installed the disk will look slimialr to one of these:
    If it is the company's machine see if they have the disks.
    Do a custom install and then use Software Update to update iPhoto to the highest version that is available.

  • Row lock class concurrency Monitor

    Greetings,
    We are getting ready to release my first webapp and need a �row-locking� class of sorts. For example, when a user logs in they have choices of searches where they can query the db. There is a link in the reulstSet that brings them to a detail screen. If that �detail� or row is currently open in a browser, I want to know about it.
    I wonder about when a user closes their browser from the �detail� view, how can I know?
    I am thinking about using a filter to track all active 'rows' and using an application scope class to track the 'locked' rows. I am developing on a Mainframe db (DMSii) that has essentially no modern capabilities. We use struts as a framework and EAServer as a container.
    I could be so far from base that I am a spectator, so if there is any CONSTRUCTIVE advice or examples to point to, It would be greatly appreciated.
    thanks,
    mccools

    You can use Rowset, RowsetEvent and RowSetListener implementation for tracking the rows.
    "I wonder about when a user closes their browser from the �detail� view, how can I know?"
    For this you can use the SessionBindingListener implementation.
    Hope this helps.
    Regards,
    Rohan Kamat

  • Question about Table Locks

    Hi All,
    Is there any Oracle defined table/place from where we can know the details about Tables that have been locked in the current session?
    Thank you.

    Hi ,
    To know which all objects are locked, you can use the below query :-
    COLUMN owner FORMAT A20
    COLUMN username FORMAT A20
    COLUMN object_owner FORMAT A20
    COLUMN object_name FORMAT A30
    COLUMN locked_mode FORMAT A15
    SELECT b.session_id AS sid,
           NVL(b.oracle_username, '(oracle)') AS username,
           a.owner AS object_owner,
           a.object_name,
           Decode(b.locked_mode, 0, 'None',
                                 1, 'Null (NULL)',
                                 2, 'Row-S (SS)',
                                 3, 'Row-X (SX)',
                                 4, 'Share (S)',
                                 5, 'S/Row-X (SSX)',
                                 6, 'Exclusive (X)',
                                 b.locked_mode) locked_mode,
           b.os_user_name
    FROM   dba_objects a,
           v$locked_object b
    WHERE  a.object_id = b.object_id
    ORDER BY 1, 2, 3, 4;Anand

  • Re-entrant Lock and concurrency manager

    Has anyone ported concurrency manager to use Re-entrant locks (jdk 1.5 constructs), would like to hear your experience.
    thanks

    Hello,
    I'm not sure what you mean. The ConcurrencyManager is designed so that it is never waiting for a lock it already holds. If it is waiting for a lock, it is because another thread is holding it, presumably because it is building the object associated to the cache key. What version are you using, and what is the lock waiting for? Using printIdentityMapLocks on the identitymapAccessor will show the locks involved on the associated session.
    Best Regards,
    Chris

  • Quick question about Auto-lock settings

    Hello all,
    I recently was able to sync my office email account to my iPhone using Microsoft Exchance active sync. The problem as I understand from searching through different posts, is that I can no longer set my Auto-lock setting to "Never" because of this, only 5 minutes (sort of a security thing as far as I can tell).
    The reason I am trying to do this is that I like to use one or two of the GPS golf apps out there, and they stay dialed in if the phone doesn't "sleep" while I am playing a round, so I would like to keep it on the entire time.
    Does anyone have any ideas of how I might be able to work around this issue in any way?
    Responses are, as always, much appreciated!
    Cheers,
    Kev

    More than likely, you could not simply delete the account. The thing is that the people who came up with the security protocols already thought of that. As an employee using a company cell phone, I could delete my account, access features or information my company restricts and then readd the account, thus bypassing security protocols.
    In addition, I might have other sensitive company information (in the form of contacts, documents, notes, etc) that would still be on the phone even if I delete my exchange account. Without autolock, people would still have access to this data even if I delete the account.
    To prevent this type of abuse, when an exchange account is added, it installs a set of restrictions and protocols to the phone. Those protocols can, at the discretion of IT, be non alterable and non removable, even when the exchange account is deleted. This prevents people from abusing the loophole you are describing.
    Similarly, simply switching to another email account will not bypass them - these protocols are now part of how your phone acts and can not be changed without wiping it back to factory settings.

  • Optimistic locking/ data concurrency and consistency

    Hi,
    When creating a tabular form using wizard, apex creates its own processes for optimistic locking. But when creating a form on a table, it doesn't create the processes. I suppose I need to create them manually.
    Can anybody show me the steps?
    Thanks!

    Scott,
    Thanks once again.
    2 things:
    My understanding is that I can't change the error message, and that if I want to customize the error message, I need to create my own process for checksum. Is this true? If so, what is the easiest way of doing this?
    I realized it returns an error message and the changes the user made gets lost if he went out for lunch and somebody else made an update on that record. What can I do so the user doesn't lose the data?
    Thanks,

  • Is there any known issue about mutex lock used in JNI on Solaris 10?

    Dear Oracle,
    I'm facing a strange behavior when running a JNI application that use the mutex lock. The application hanged after running for a while on one of our Solaris 10 machines. After investigating the stack trace of the application, it shows that one of all the application's threads that access the JNI hold the mutex lock forever, and that cause the other threads cannot access the JNI. The same application works fine on other Solaris 10 machines.
    The mutex function I used as show below:
    mutex_lock(&mutex)
    The application hanged at this line:
    0xff2cc940 ___lwp_mutex_timedlock + 0x8
    The Kernel version is SunOS 5.10 Generic_142900-14
    Since this issue occurrs on only one Solaris 10 machine in our environment, we are wondering if there is any known issue regarding the mutex lock used in JNI on Solaris 10. We tried to search the Internet and the Java Bug database but found nothing related to this issue.
    Best regards,
    Krit K.

    876587 wrote:
    ... and that cause the other threads cannot access the JNI. The same application works fine on other Solaris 10 machines.
    Different environments mean just that. So something could be causing it outside java.
    Additionally if anything at all is different in the execution data, such as even a name having a different size, then it would change the execution path.
    Which would cause a problem in the JNI code to manifest itself on only one box. Probably isn't a pointer bug but all sorts of odd behavior can result from that.

  • Problem about InfoObject locked by conversion

    Hi Experts:
       Now I am doing BW Upgrade Project and one task is to check all infoobjects consistency (RSUPGRCHECK).
    And one infoobject 0MATERIAL is inconsistent.
    1. I have used RSD1 to repair it, but it failed
    2. When I tried to activate manually, but it popup with message that it is locked by conversion.
    3. I tried to use SE14 to delete /BI0/PMATERIAL data, but the option for "activate and delete data" is greyed out.
       This is my situation now, any one could give me some suggestion?
       Thank you so much,
       Louis Lu

    Hi,
    I RSA1, click on Tab Find, type ur IO name, then down, check on the box All Object Types. It vll show u where all u used that IO, then delete them accordingly.
    or
    goto T-code RSDMD, type ur IO name, and delete manually.
    or
    Goto T-code SLG1, In object type RSDMD, in sub object type
    MD_DEL, then type ur user name, then execute.,,u vll c what all data u have loaded so far, delete what is pertaining to ur IO
    try to delete the content as u are unable to perform the same with SE14... and then try repairing the same.. and perform ur steps..
    Thanks

  • 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

  • Info about network lock

    Hi all,
    if I buy iPhone 4 as a gift, is it still locked on AT&T or is it never locked? Because the person I'm sending it to doesn't live in the U.S. and the mobile carrier for him is Vodafone. Thanks folks.

    It depends on the country in which you buy the iphone.
    ALL U.S. iphones are carrier locked to AT&T and cannot be activated with any other carrier in any other country.

  • About to lock a specific app on the phone

    I would like to know if there is a way to make a lock on a app. Like i have the amazon app in i want it to be locked is there a way to lock a specific app?

    No

  • Question about object lock

    Hello everyone,
    I want to know which method owns the lock of a specific object (some of my methods are blocked because they can not achieve the lock of a specific object, and I am wondering which method owns the lock to solve the deadlock issue). Does anyone know how to get the information?
    Thanks in advance,
    George

    Thanks Feamar,
    U should not think of a method as having a lock,
    rather than that u should think of a piece of
    synchronized code to have the lock on an object.
    U should try and find nested synchronization
    statements in your code, those are risky and can lead
    to deadlocks.
    Good luckYour reply is very helpful. What means "nested synchronization statement" in your reply?
    regards,
    George

Maybe you are looking for

  • Error in Uploading BOM

    Dear Gurus'                       While uploading  BOM i came across an erros which says unit of measure EA doesn't specify decimal places in EA. what this error is and how i need to sort it out? i'm uploading alternative BOM 3. Thanks & regard Praka

  • PI 71. System copy

    I am copying my PI Production system back to my Integration test system. I have read the System copy guide(s) and concermed that I may select wrong method. I am going to copy ABAP via  DB Oracle tools. But the java side we are not sure how to proceed

  • Migration assistant does not allow selection of backup

    The HD on my MBP failed.  Under warranty. So made an appointment and took her in to my closest Apple Store.  I had a recent full backup on my Time Capsule and thought I would be bringing the "not quite dead yet" old drive home.  Wrong.  Bringing the

  • V$undostat

    hi, Good day. DB version is 10.2.0.4 SQL> select MAX(maxquerylen) from v$undostat; MAX(MAXQUERYLEN)            59389 1 row selected. SQL> show parameter undo_retention NAME                                 TYPE                           VALUE undo_ret

  • Why won't OS X Leopard install on my iBook G4

    What I did: I was trying to install Leopard on my iBook G4, and it said as one of the trouble shooting steps that I should erase my disk (Macintosh HD) so I did, and it still didn't work, so now I am stuck with a empty hard drive. is there any way I