Monday, October 17, 2011

Java: Mutual Exclusive Lock based on Priority - PriorityLock

Hey all,

I'm back with a Java related post now. We know that there is a Lock implementation ReentrantLock which provides the locking mechanism similar as synchronized statements & methods with some extended capabilities and Condition support. But the problem with the synchronized statements, methods & Lock is that - we don't have any control over which thread is going to acquire the lock; we just know that one of the waiting threads will get it. What if I know that there are some threads which are executing a higher priority Use-Case and I need one of those high-priority threads to acquire the lock?

Well - I searched on Web and didn't find any class which can help me here. So, I just thought of writing my own class for this purpose - PriorityLock.

So, what should be my PriorityLock?

A mutual exclusive Lock which will guarantee that requests with higher priority value gets the Lock first before a lower priority request is given with the lock. If a higher priority request comes when a lower priority request was granted the lock in past and is not available, it will not be revoked. Priority will be considered & guaranteed only when the lock is available.

Good, I defined my class objective. But now - How the lock acquisition method will know the thread priority?

A method parameter

OK, what is it?

A simple integer should be working fine. But, how easy is it to manage the priorities if it is a simple integer? I feel it is bit hard to manage priorities with just integer as Constants in a considerably complex application. So, I decided to come up with an Interface which just has a single method providing the priority as Integer value. How this makes the priority management easy? Well, we can create enum implementing the interface & collect all the Priority values inside a single enum – Simple & Clean.

We just have one more decision to make. What if there are multiple requests waiting for the lock with same priority? For most of the applications, First-In-First-Out should be working fine; so we will take that here too.

Good. We have some clear idea on what needs to be done. With this in mind, I have implemented PriorityLock and also made an Application for demonstrating it. If you’re interested, feel free to try out the PriorityLock & DemoApp from the following URLs.

PriorityLock JavaDoc:
PriorityLock Library:
PriorityLock Demo Application:
prioritylock-demo.jar has the source & class files for the Demo application. You need prioritylock-bin.jar in your classpath for running this demo app.

Though the PriorityLock is well tested, it would be great, if people can test & post bugs, if found any for fixing & publishing them. (

Currently, my PriorityLock is not an implementation of Lock Interface, as the lock method of Lock interface doesn't accept any parameter. But, I am improving it with more methods providing similar behaviours as mentioned in Lock interface. It'll be enhanced as Lock interface implementation soon.

That’s it for today – Bye for now.

No comments:

Post a Comment