Results 1 to 8 of 8

Thread: Releasing lock in Java

  1. #1
    Join Date
    Sep 2011
    Location
    South Africa
    Beans
    165
    Distro
    Xubuntu 12.10 Quantal Quetzal

    Releasing lock in Java

    Hi

    I have a somewhat simple problem. I have a synchronized method, I want to release the lock when a specific condition is met. How do I do that?

    Code:
    public synchronized void someMethod(){
               if (empty>0){
                       --empty;
                }
                else{
                      //releaseLock here
                      doSomeOtherCrazyThing();
                      }
    }
    Regards,

    A
    Last edited by 3v3rgr33n; March 17th, 2013 at 05:38 PM.

  2. #2
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,664
    Distro
    Ubuntu 13.10 Saucy Salamander

    Re: Releasing lock in Java

    Could you use a synchronized block rather than making the whole method synchronized?

    Code:
    public void someMethod() {
    
        synchronized(self) {
            if (empty > 0) {
                --empty;
                return;
            }
        }
    
        doSomeOtherCrazyThing();
    
    }
    Alternatively hand-code something with an explicit lock rather than using the synchronized keyword:

    http://docs.oracle.com/javase/7/docs...ocks/Lock.html
    Please create new threads for new questions.
    Please wrap code in code tags using the '#' button or enter it in your post like this: [code]...[/code].

  3. #3
    Join Date
    Sep 2011
    Location
    South Africa
    Beans
    165
    Distro
    Xubuntu 12.10 Quantal Quetzal

    Re: Releasing lock in Java

    Thnx!

  4. #4
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,123
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: Releasing lock in Java

    Quote Originally Posted by 3v3rgr33n View Post
    Hi

    I have a somewhat simple problem. I have a synchronized method, I want to release the lock when a specific condition is met. How do I do that?

    Code:
    public synchronized void someMethod(){
               if (empty>0){
                       --empty;
                }
                else{
                      //releaseLock here
                      doSomeOtherCrazyThing();
                      }
    }
    Regards,

    A
    It looks like you need a synchronized method to update the value of 'empty', and call that method from other unsynchronized methods..

  5. #5
    Join Date
    Sep 2011
    Location
    South Africa
    Beans
    165
    Distro
    Xubuntu 12.10 Quantal Quetzal

    Re: Releasing lock in Java

    I don't understand what you're trying to say.
    Last edited by 3v3rgr33n; March 17th, 2013 at 05:13 AM.

  6. #6
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,664
    Distro
    Ubuntu 13.10 Saucy Salamander

    Re: Releasing lock in Java

    I presume something like this?

    Code:
    /**
     * Decrement empty
     * @returns true if empty could be decremented, i.e. was > 0
     */
    public synchronized boolean decrementEmpty() {
        if (empty > 0) {
            --empty;
            return true;
        }
        return false;
    }
    
    public void crazyUnsynchronizedMethod() {
        if (!decrementEmpty()) {
            doSomeOtherCrazyThing();
        }
    }
    The advantage would be that it doesn't conflate the manipulation of empty with doing some other crazy thing -- you might want to decrement empty from somewhere else and not tie that to doing some other crazy thing. With a contrived example like this, it's difficult to say which approach makes most sense. (Or, indeed, whether the return value from the first method should indicate whether empty ends up as zero rather than whether empty could be decremented).
    Please create new threads for new questions.
    Please wrap code in code tags using the '#' button or enter it in your post like this: [code]...[/code].

  7. #7
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,123
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: Releasing lock in Java

    Quote Originally Posted by r-senior View Post
    I presume something like this?
    With a contrived example like this, it's difficult to say which approach makes most sense..
    IMHO the other method never makes sense, and leads to the original question. Synchronized methods should be short and focused. If your design doesn't let you write such methods, reconsider the design...

  8. #8
    Join Date
    Sep 2011
    Location
    South Africa
    Beans
    165
    Distro
    Xubuntu 12.10 Quantal Quetzal

    Re: Releasing lock in Java

    Quote Originally Posted by ofnuts View Post
    IMHO the other method never makes sense, and leads to the original question. Synchronized methods should be short and focused. If your design doesn't let you write such methods, reconsider the design...
    Best advice ever!

    I changed the structure. Now works. Thanx guys

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •