Sunday, June 9, 2019

Lock In Java Example

Lock is an interface in java and ReentrantLock is an implementation class of lock interfac.

ReadWriteData.java
package com.shubh.lock.vs.synchronization;

import java.util.Date;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ReadWriteData {

 private final Lock lockOnWrite = new ReentrantLock();
 private final Lock lockOnRead = new ReentrantLock();

 public void writeData(Object document) {
  final Lock displayLock = this.lockOnWrite;
  displayLock.lock();
  try {
   Long duration = (long) (Math.random() * 10000);
   System.out.println(Thread.currentThread().getName() + ": Resource(ReadWriteData) : write a Job" + " during "
     + (duration / 1000) + " seconds :: Start Time At - " + new Date());
   Thread.sleep(duration);
  } catch (InterruptedException e) {
   e.printStackTrace();
  } finally {
   System.out.printf("%s: The document has been" + " written\n", Thread.currentThread().getName());
   displayLock.unlock();
  }
 }

 public void readData(Object document) {
  final Lock readQueueLock = this.lockOnRead;
  readQueueLock.lock();
  try {
   Long duration = (long) (Math.random() * 10000);
   System.out.println(Thread.currentThread().getName() + ": Resource(ReadWriteData) : read a Job during "
     + (duration / 1000) + " seconds :: Start At - " + new Date());
   Thread.sleep(duration);
  } catch (InterruptedException e) {
   e.printStackTrace();
  } finally {
   System.out.printf("%s: The document has" + " been read\n", Thread.currentThread().getName());
   readQueueLock.unlock();
  }
 }
}

ReadJob.java

package com.shubh.lock.vs.synchronization;

public class ReadJob implements Runnable {

 private ReadWriteData readWriteData;

 ReadJob(ReadWriteData tr) {
  this.readWriteData = tr;
 }

 @Override
 public void run() {
  System.out.println("read job");
  readWriteData.readData(new Object());
 }
}

WriteJob.java

package com.shubh.lock.vs.synchronization;

public class WriteJob implements Runnable {

 private ReadWriteData readWriteData;
 
 WriteJob(ReadWriteData tr) {
  this.readWriteData = tr;
 }

 @Override
 public void run() {
  System.out.println("write job");
  readWriteData.writeData(new Object());
 }
}

LockTest.java

package com.shubh.lock.vs.synchronization;

public class LockTest {
 public static void main(String[] args) {
  ReadWriteData readWriteData = new ReadWriteData();
  Thread thread[] = new Thread[10];
  
  for (int i = 5; i < 10; i++) {
   thread[i] = new Thread(new ReadJob(readWriteData), "Thread " + i);
  }
  
  for (int i = 0; i < 5; i++) {
   thread[i] = new Thread(new WriteJob(readWriteData), "Thread " + i);
  }
  
  for (int i = 0; i < 10; i++) {
   thread[i].start();
  }
 }

}

  Output:  

write job
read job
write job
write job
write job
write job
read job
read job
read job
read job
Thread 0: Resource(ReadWriteData) : write a Job during 8 seconds :: Start Time At - Sun Jun 09 18:19:48 IST 2019
Thread 5: Resource(ReadWriteData) : read a Job during 2 seconds :: Start At - Sun Jun 09 18:19:48 IST 2019
Thread 5: The document has been read
Thread 6: Resource(ReadWriteData) : read a Job during 1 seconds :: Start At - Sun Jun 09 18:19:51 IST 2019
Thread 6: The document has been read
Thread 7: Resource(ReadWriteData) : read a Job during 1 seconds :: Start At - Sun Jun 09 18:19:53 IST 2019
Thread 7: The document has been read
Thread 8: Resource(ReadWriteData) : read a Job during 2 seconds :: Start At - Sun Jun 09 18:19:54 IST 2019
Thread 0: The document has been written
Thread 1: Resource(ReadWriteData) : write a Job during 6 seconds :: Start Time At - Sun Jun 09 18:19:57 IST 2019
Thread 8: The document has been read
Thread 9: Resource(ReadWriteData) : read a Job during 0 seconds :: Start At - Sun Jun 09 18:19:57 IST 2019
Thread 9: The document has been read
Thread 1: The document has been written
Thread 2: Resource(ReadWriteData) : write a Job during 4 seconds :: Start Time At - Sun Jun 09 18:20:03 IST 2019
Thread 2: The document has been written
Thread 3: Resource(ReadWriteData) : write a Job during 8 seconds :: Start Time At - Sun Jun 09 18:20:08 IST 2019
Thread 3: The document has been written
Thread 4: Resource(ReadWriteData) : write a Job during 8 seconds :: Start Time At - Sun Jun 09 18:20:16 IST 2019
Thread 4: The document has been written

No comments:

Post a Comment