6

I am very poor in MultiThreading concepts of Java.

I was going through ReentrantLock features and usage. I got that it is more flexible then synchronized and adds few more features.

I can see the examples mentioned on it and I got it well.

I am not able to figure out the real time scenario where exactly it will be helpful in business.

I can see that it will be best to avoid deadlocks.

Can some one provide the use case where without ReentrantLock it will be difficult to solve such use case.

or can point to some link will be helpful.

Jayesh
  • 6,047
  • 13
  • 49
  • 81
  • I am asking use case in real life(scenario) where it is must to use such lock and synchronized will not help in such situation. I am aware of ReentrantLock features. – Jayesh Nov 01 '13 at 12:26

4 Answers4

5

For a simple case how about timed lock / or partial lock for a application which demands performance.

A Very common example would be online portals which let you buy/book tickets(any). You get a timed lock on the seat/resource you are interested in. After the time expires and if transaction is not completed any other client application(thread) can acquire lock on it.

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
  • Nice, just for curiosity I am asking, is red bus/bookmyshow or any booking happens for seat in this way? – Jayesh Nov 01 '13 at 12:20
  • I would not say that their code base is in Java and they use ReentrantLock for synchronization but the mechanism is similar. – Aniket Thakur Nov 03 '13 at 07:17
  • There are no methods in [ReentrantLock](https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/ReentrantLock.html) that gives me a lock for a certain period though I have options to try for a lock for a limited time so how does timed lock use case is provided by ReentrantLock? Have I misunderstood anything? – Sabir Khan Jan 22 '17 at 06:51
  • ReentrantLock can just timed lock on one JVM, while bus/book tickets system will always not just one JVM, this kind of usecase will need the help of RDBMS. – astarring May 20 '17 at 22:30
4

ReentrantLock can be used for timeout requirements. Suppose you are trying to acquire a lock but the lock is already taken at that point of time you can use tryLock() which will return immediately and later you can try again.

ReentrantLock has some other advantage of being provide fairLock or unFair by its con structure:

public ReentrantLock(boolean fair)

if you put the fair as false than it will not provide you a lock with fair ordering. And unfair ordering out performs the fair lock. I can explain this if you need more details.

But the problem with ReentrantLock is, it is hard to debug because in the logs it can't explain about the owner of the lock but in synchronized it is possible. It's the biggest disadvantage.

you can implement features like dining philosopher with this locking mechanism. If both sticks are available than go ahead and eat else drop the one which you are currently holding.

public void run() {
    while (!Thread.currentThread().isInterrupted()) {
        if (l.getLeftLock().tryLock()) {
            try {
                if (r.getRightLock().tryLock()) {
                    try {
                        System.out.println("Eating philosopher1 !!!!");
                    } finally {
                        r.getRightLock().unlock();
                    }
                }
            } finally {
                l.getLeftLock().unlock();
            }

        }
        try {
            Thread.currentThread().sleep((int) (100 * Math.random()));
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
    }
}
Trying
  • 14,004
  • 9
  • 70
  • 110
  • what you are telling me is ReentrantLock features, I am asking use case in real life(scenario) where it is must to use such lock and synchronized will not help in such situation. – Jayesh Nov 01 '13 at 12:22
  • @Jayesh i think it's very evident form the answer. I will give you a very good example , you can implement features like dining philosopher with this locking mechanism. If both sticks are available than go ahead and eat else drop the one which you are holding. You understood? – Trying Nov 01 '13 at 12:54
2

For the ReentrantLock class, compared to plain synchronized in my opinion the key benefits are:

  • acquire/release the lock in different blocks.
  • have fair locks (FIFO for the waiting threads)
  • can try to lock
  • can have multiple condition variables

Also, a bit off-topic, but for understanding the java concurrent classes, i found Java Concurrent Animated very useful. Download the runnable jar file and see for your self.

Ortwin Angermeier
  • 5,957
  • 2
  • 34
  • 34
0

Take a look at: https://stackoverflow.com/a/1312282/668951 Also in my case I read about it in Henry Wong's book though not in great detail.

Community
  • 1
  • 1
Atul
  • 2,673
  • 2
  • 28
  • 34