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].
Bookmarks