Tuesday, October 16, 2018

what do you mean by synchronized block in java?

Problem statement: Can you explain me about synchronized block in java?

Synchronized block: A block which contains synchronized keyword that is called synchronized block.

#Synchronized block:
  1. class Institute {
  2.     public void classRoom(String facultyName) {
  3.         synchronized (Institute.class) {
  4.             for (int i = 0; i < 10; i++)
  5.                 System.out.println(i + " .class taken by " + facultyName);
  6.             try {
  7.                 Thread.sleep(1000);
  8.             } catch (InterruptedException e) {
  9.                 e.printStackTrace();
  10.             }
  11.         }
  12.     }
  13. }
  14. class MyThread extends Thread {
  15.     Institute inst;
  16.     String factName;
  17.     @Override
  18.     public void run() {
  19.         inst.classRoom(factName);
  20.     }
  21.     MyThread(Institute inst, String name) {
  22.         this.inst = inst;
  23.         this.factName = name;
  24.     }
  25. }
  26. public class SynchronizedExe {
  27.     public static void main(String args[]) {
  28.         Institute inst1 = new Institute();
  29.         Institute inst2 = new Institute();
  30.         MyThread t1 = new MyThread(inst1, "Madhusmita");
  31.         MyThread t2 = new MyThread(inst2, "Ishaan");
  32.         t1.start();
  33.         t2.start();
  34.     }
  35. }
Output:
0 .class taken by Ishaan
1 .class taken by Ishaan
2 .class taken by Ishaan
3 .class taken by Ishaan
4 .class taken by Ishaan
5 .class taken by Ishaan
6 .class taken by Ishaan
7 .class taken by Ishaan
8 .class taken by Ishaan
9 .class taken by Ishaan
0 .class taken by Madhusmita
1 .class taken by Madhusmita
2 .class taken by Madhusmita
3 .class taken by Madhusmita
4 .class taken by Madhusmita
5 .class taken by Madhusmita
6 .class taken by Madhusmita
7 .class taken by Madhusmita
8 .class taken by Madhusmita
9 .class taken by Madhusmita

#Without synchronized block
  1. class Institute {
  2.     public void classRoom(String facultyName) {
  3.         for (int i = 0; i < 10; i++)
  4.             System.out.println(i + " .class taken by " + facultyName);
  5.         try {
  6.             Thread.sleep(1000);
  7.         } catch (InterruptedException e) {
  8.             e.printStackTrace();
  9.         }
  10.     }
  11. }
  12. class MyThread extends Thread {
  13.     Institute inst;
  14.     String factName;
  15.     @Override
  16.     public void run() {
  17.         inst.classRoom(factName);
  18.     }
  19.     MyThread(Institute inst, String name) {
  20.         this.inst = inst;
  21.         this.factName = name;
  22.     }
  23. }
  24. public class SynchronizedExe {
  25.     public static void main(String args[]) {
  26.         Institute inst1 = new Institute();
  27.         Institute inst2 = new Institute();
  28.         MyThread t1 = new MyThread(inst1, "Madhusmita");
  29.         MyThread t2 = new MyThread(inst2, "Ishaan");
  30.         t1.start();
  31.         t2.start();
  32.     }
  33. }
Output:
0 .class taken by Madhusmita
1 .class taken by Ishaan
2 .class taken by Madhusmita
3 .class taken by Ishaan
4 .class taken by Madhusmita
5 .class taken by Ishaan
---------------------------------------
#With synchronized method
  1. class Institute {
  2.     synchronized public void classRoom(String facultyName) {
  3.         for (int i = 0; i < 10; i++)
  4.             System.out.println(i + " .class taken by " + facultyName);
  5.         try {
  6.             Thread.sleep(1000);
  7.         } catch (InterruptedException e) {
  8.             e.printStackTrace();
  9.         }
  10.     }
  11. }
  12. class MyThread extends Thread {
  13.     Institute inst;
  14.     String factName;
  15.     @Override
  16.     public void run() {
  17.         inst.classRoom(factName);
  18.     }
  19.     MyThread(Institute inst, String name) {
  20.         this.inst = inst;
  21.         this.factName = name;
  22.     }
  23. }
  24. public class SynchronizedExe {
  25.     public static void main(String args[]) {
  26.         Institute inst1 = new Institute();
  27.         Institute inst2 = new Institute();
  28.         MyThread t1 = new MyThread(inst1, "Madhusmita");
  29.         MyThread t2 = new MyThread(inst2, "Ishaan");
  30.         t1.start();
  31.         t2.start();
  32.     }
  33. }
Output:
0 .class taken by Madhusmita
0 .class taken by Ishaan
1 .class taken by Madhusmita
1 .class taken by Ishaan
2 .class taken by Madhusmita
2 .class taken by Ishaan

No comments:

Post a Comment

How to run standalone mock server on local laptop

 Please download the standalone wiremock server from Direct download section at the bottom of the page.  Download and installation Feel fre...