What are applications of ReEntrantLock in java


In previous thread concurrency tutorial we learned what is Locks and ReEntrantLocks in java and in this thread concurrency tutorial we will learn what are application of using ReentrantLock in real world with program in thread concurrency java.



Application of using ReentrantLock in real world with program in java >

Reentrantlocks can be used to solve Train-passenger problem when 2 passengers try to book train ticket, dat too when only 1 ticket is available.

Program/Example to show that with Reentrantlocks no problems will happen when  2 passengers try to book train ticket, when only 1 ticket was available >
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class ReentrantLockTest {
   public static void main(String[] args) {
          Lock lock=new ReentrantLock();
          MyRunnable myRunnable=new MyRunnable(lock);
          new Thread(myRunnable,"Passenger1 Thread").start();
          new Thread(myRunnable,"Passenger2 Thread").start();
         
   }
}


class MyRunnable implements Runnable{
  
   int ticketsAvailable=1;
   Lock lock;
   public MyRunnable(Lock lock) {
          this.lock=lock;
   }
  
   public void run(){
         
          System.out.println("Waiting to book ticket for : "+
                              Thread.currentThread().getName());
         
          lock.lock();
         
          if(ticketsAvailable>0){
                 System.out.println("Booking ticket for : "+
                                     Thread.currentThread().getName());
                
                 //Let's say system takes some time in booking ticket
                 //(here we have taken 1 second time)
                 try{
                       Thread.sleep(1000);
                 }catch(Exception e){}
                  
                 ticketsAvailable--;
                 System.out.println("Ticket BOOKED for : "+
                                     Thread.currentThread().getName());
                 System.out.println("currently ticketsAvailable = "+ticketsAvailable);
          }
          else{
                 System.out.println("Ticket NOT BOOKED for : "+
                                     Thread.currentThread().getName());
          }
         
         
          lock.unlock();    //read explanation for 5sec
   }
}
/*OUTPUT
Waiting to book ticket for : Passenger1 Thread
Waiting to book ticket for : Passenger2 Thread
Booking ticket for : Passenger1 Thread
Ticket BOOKED for : Passenger1 Thread
currently ticketsAvailable = 0
Ticket NOT BOOKED for : Passenger2 Thread
*/


OUTPUT ANALYZATION of above program >
Waiting to book ticket for : Passenger1 Thread
Waiting to book ticket for : Passenger2 Thread
first Passenger1 Thread and Passenger2 Thread waited to book tickets.

Booking ticket for : Passenger1 Thread
Than, Passenger1 Thread acquired lock [by calling lock.lock() ], but Passenger2 Thread wasn’t able to acquire lock and was waiting for Passenger1 Thread to release lock.

Ticket BOOKED for : Passenger1 Thread
Passenger1 Thread was successfully able to book ticket and reduce the available ticket count to 0. Than it released object lock  [by calling lock.unlock() ]
currently ticketsAvailable = 0

Ticket NOT BOOKED for : Passenger2 Thread
Than, Passenger1 Thread acquired lock [by calling lock.lock() ], but available ticket count at that time was 0 so it wasn’t able to book ticket.





Summary >

In previous thread concurrency tutorial we learned what is Locks and ReEntrantLocks in java and in this thread concurrency tutorial we learned what are application of using ReentrantLock in real world with program in thread concurrency java.


Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.




RELATED LINKS>

Lock and Reentrant Locks >

Locks and ReEntrantLocks in thread concurrency  in java

Implementation of custom/own Lock and ReEntrantLock in java


ReentrantLock class provides implementation of Lock’s newCondition() method - description and solving producer consumer program using this method



ReadWriteLock and ReentrantReadWriteLock >

ReadWriteLock and ReentrantReadWriteLock in thread concurrency  in java

Implementation of custom/own ReadWriteLock and ReentrantReadWriteLock in java



Thread concurrency Interviews >

Must read for you :
Must read for you :

No comments:

Post a Comment