Showing posts with label Mutex. Show all posts
Showing posts with label Mutex. Show all posts

August 27, 2019

Part 10: Java Thread Interview Questions & Answers (ReentrantLock and ReentrantReadWriteLock)

What are Reentrant Locks?

  • ReentrantLock in Java is added on java.util.concurrent package in Java 1.5 along with other concurrent utilities like CountDownLatch, Executors, and CyclicBarrier.
  • ReentrantLock is a concrete implementation of the Lock interface provided in the Java concurrency package from Java 1.5 onwards. 
  • Reentrant Locks are provided in Java to provide synchronization with greater flexibility.
  • The code which manipulates the shared resource is surrounded by the calls to lock and unlock method. This gives a lock to the current working thread and blocks all other threads which are trying to take a lock on the shared resource.
  • A ReentrantLock allows threads to enter into the lock on a resource more than once. When the thread first enters into the lock, a hold count is set to one. Before unlocking the thread can re-enter into lock again and every time hold count is incremented by one. For every un-lock request, the hold count is decremented by one and when the hold count is 0, the resource is unlocked.
  • Fairness parameter is provided while creating an instance of ReentrantLock in the constructor. The fairness parameter used to construct the lock object decreases the throughput of the program.
  • ReentrantLock provides the same visibility and ordering guarantee, provided by implicitly locking, which means, unlock() happens before another thread gets lock().
  • If you forget to call the unlock() method in the finally block, it will lead to bugs in the program. Make sure that the lock is released before the thread exits.

Part 9: Java Thread Interview Questions & Answers (All About Java Semaphore and Mutex)

Why do we need Semaphore?

  • Let's say our application calls a service, which is quite slow. Because the service is slow, only 5 calls can be made at a time.
  • Now let's say their are 50 threads present in the application, and they all try to access the service. We need a mechanism, by which we can restrict the call to slow service. This can be achieved using Semaphore.
  • A Semaphore will act as a layer, with a limited number of permits (in our case permits=5). 
  • A thread 1 wants to access the service, it will call the acquire() method to get the permit. Semaphore will check the number of available permits (in our case it's 5), if it's available it will give a permit to thread 1 and then decrease the permit by 1 (now the permit=4).
  • While thread 1 is accessing the slow service, thread 2 comes. The Semaphore again checks the available permit (which is 4) and gives it to thread 2.
  • Let's say threads 3, 4 and 5 come and get the permit. While these 5 threads are accessing the slow service thread 6 comes, since now the permit is 0 Semaphore will block thread 6.
  • In the meantime, Thread 1 completes the work from slow service, it will call the release() method. The release() method will increment the permit(which was 0) by 1. Now the permit is 1, and the Semaphore gives access to Thread 6 and decrements the permit (new value will be 0).