Sunday, June 9, 2019

Method And Block Synchronization

ReadWriteDataSyn.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 ReadWriteDataSyn {


 /*synchronized public void writeData(Object document) {
  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());
  }
 }

 synchronized public void readData(Object document) {
  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());
  }
 }*/
 
 
 public void writeData(Object document) {
  try {
   synchronized (this) {
    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());
  }
 }

 public void readData(Object document) {
  try {
   synchronized (this) {
    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());
  }
 }
}

ReadJobSyn.java

package com.shubh.lock.vs.synchronization;

public class ReadJobSyn implements Runnable {

 private ReadWriteDataSyn readWriteData;

 ReadJobSyn(ReadWriteDataSyn tr) {
  this.readWriteData = tr;
 }

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

WriteJobSyn.java

package com.shubh.lock.vs.synchronization;

public class WriteJobSyn implements Runnable {

 private ReadWriteDataSyn readWriteData;

 WriteJobSyn(ReadWriteDataSyn tr) {
  this.readWriteData = tr;
 }

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

SynchronizedTest.java

package com.shubh.lock.vs.synchronization;

public class SynchronizedTest {
 public static void main(String[] args) {
  ReadWriteDataSyn readWriteData = new ReadWriteDataSyn();
  Thread thread[] = new Thread[10];

  for (int i = 5; i < 10; i++) {
   thread[i] = new Thread(new ReadJobSyn(readWriteData), "Thread " + i);
  }

  for (int i = 0; i < 5; i++) {
   thread[i] = new Thread(new WriteJobSyn(readWriteData), "Thread " + i);
  }

  for (int i = 0; i < 10; i++) {
   thread[i].start();
  }
 }

}

 Output: 

write job
write job
write job
read job
read job
read job
read job
write job
read job
write job
Thread 1: Resource(ReadWriteData) : write a Job during 9 seconds :: Start Time At - Sun Jun 09 18:38:15 IST 2019
Thread 1: The document has been written
Thread 4: Resource(ReadWriteData) : write a Job during 1 seconds :: Start Time At - Sun Jun 09 18:38:25 IST 2019
Thread 4: The document has been written
Thread 2: Resource(ReadWriteData) : write a Job during 2 seconds :: Start Time At - Sun Jun 09 18:38:26 IST 2019
Thread 2: The document has been written
Thread 6: Resource(ReadWriteData) : read a Job during 8 seconds :: Start At - Sun Jun 09 18:38:28 IST 2019
Thread 6: The document has been read
Thread 7: Resource(ReadWriteData) : read a Job during 5 seconds :: Start At - Sun Jun 09 18:38:37 IST 2019
Thread 7: The document has been read
Thread 8: Resource(ReadWriteData) : read a Job during 5 seconds :: Start At - Sun Jun 09 18:38:43 IST 2019
Thread 8: The document has been read
Thread 5: Resource(ReadWriteData) : read a Job during 5 seconds :: Start At - Sun Jun 09 18:38:48 IST 2019
Thread 5: The document has been read
Thread 9: Resource(ReadWriteData) : read a Job during 0 seconds :: Start At - Sun Jun 09 18:38:53 IST 2019
Thread 9: The document has been read
Thread 0: Resource(ReadWriteData) : write a Job during 1 seconds :: Start Time At - Sun Jun 09 18:38:54 IST 2019
Thread 0: The document has been written
Thread 3: Resource(ReadWriteData) : write a Job during 4 seconds :: Start Time At - Sun Jun 09 18:38:56 IST 2019
Thread 3: The document has been written



Related Tutorials
 

No comments:

Post a Comment