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:
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
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
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
Synchronized block: A block which contains synchronized keyword that is called synchronized block.
#Synchronized block:
- class Institute {
- public void classRoom(String facultyName) {
- synchronized (Institute.class) {
- for (int i = 0; i < 10; i++)
- System.out.println(i + " .class taken by " + facultyName);
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
- class MyThread extends Thread {
- Institute inst;
- String factName;
- @Override
- public void run() {
- inst.classRoom(factName);
- }
- MyThread(Institute inst, String name) {
- this.inst = inst;
- this.factName = name;
- }
- }
- public class SynchronizedExe {
- public static void main(String args[]) {
- Institute inst1 = new Institute();
- Institute inst2 = new Institute();
- MyThread t1 = new MyThread(inst1, "Madhusmita");
- MyThread t2 = new MyThread(inst2, "Ishaan");
- t1.start();
- t2.start();
- }
- }
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
- class Institute {
- public void classRoom(String facultyName) {
- for (int i = 0; i < 10; i++)
- System.out.println(i + " .class taken by " + facultyName);
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- class MyThread extends Thread {
- Institute inst;
- String factName;
- @Override
- public void run() {
- inst.classRoom(factName);
- }
- MyThread(Institute inst, String name) {
- this.inst = inst;
- this.factName = name;
- }
- }
- public class SynchronizedExe {
- public static void main(String args[]) {
- Institute inst1 = new Institute();
- Institute inst2 = new Institute();
- MyThread t1 = new MyThread(inst1, "Madhusmita");
- MyThread t2 = new MyThread(inst2, "Ishaan");
- t1.start();
- t2.start();
- }
- }
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
- class Institute {
- synchronized public void classRoom(String facultyName) {
- for (int i = 0; i < 10; i++)
- System.out.println(i + " .class taken by " + facultyName);
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- class MyThread extends Thread {
- Institute inst;
- String factName;
- @Override
- public void run() {
- inst.classRoom(factName);
- }
- MyThread(Institute inst, String name) {
- this.inst = inst;
- this.factName = name;
- }
- }
- public class SynchronizedExe {
- public static void main(String args[]) {
- Institute inst1 = new Institute();
- Institute inst2 = new Institute();
- MyThread t1 = new MyThread(inst1, "Madhusmita");
- MyThread t2 = new MyThread(inst2, "Ishaan");
- t1.start();
- t2.start();
- }
- }
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