Java — Synchronization Method | Code Factory

package com.example.thread;/**
* @author code.factory
*
*/
public class SynchronizedTest {
public static void main(String... args) {
Display d = new Display();
MyThread t1 = new MyThread(d, "Code");
MyThread t2 = new MyThread(d, "Factory");
t1.start();
t2.start();
}
}
class Display {
public synchronized void wish(String name) {
for(int i=0; i<5; i++) {
System.out.print("Display ");
try {
Thread.sleep(1000);
} catch(Exception e) {
e.printStackTrace();
}
System.out.println(name);
}
}
}
class MyThread extends Thread {
Display d;
String name;
public MyThread(Display d, String name) {
this.d = d;
this.name = name;
}
public void run() {
d.wish(name);
}
}
Display Code
Display Code
Display Code
Display Code
Display Code
Display Factory
Display Factory
Display Factory
Display Factory
Display Factory
  • If we are not declaring wish() method as Synchronized then both threads will be executed simultaneously and hence we will get irregular output.
  • If we declare wish() method as synchronized then at a time only 1 thread is allow to execute wish() method on the given display object. Hence we will get regular output.
package com.example.thread;/**
* @author code.factory
*
*/
public class SynchronizedTest {
public static void main(String... args) {
Display d1 = new Display();
Display d2 = new Display();
MyThread t1 = new MyThread(d1, "Code");
MyThread t2 = new MyThread(d2, "Factory");
t1.start();
t2.start();
}
}
class Display {
public synchronized void wish(String name) {
for(int i=0; i<5; i++) {
try {
Thread.sleep(1000);
} catch(Exception e) {
e.printStackTrace();
}
System.out.println(name);
}
}
}
class MyThread extends Thread {
Display d;
String name;
public MyThread(Display d, String name) {
this.d = d;
this.name = name;
}
public void run() {
d.wish(name);
}
}
Code
Factory
Code
Factory
Factory
Code
Code
Factory
Code
Factory
  • Even though wish() method is Synchronized we will get irregular output because threads are operating on different Java objects.
  • Conclusion : If multiple threads are operating on same Java object then Synchronization is required. If multiple threads are operating on mutiple Java objects Synchronization is not required.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Effortless file uploads with ActiveStorage, Apollo and React

Happy Number (Leet Code)

TryHackMe: Network Services writeup

Container Platform Security at Cruise

Kubernetes Logo in Armor

An introduction to Terraform for AWS

What is Honeygain?

The differences between static and dynamic libraries

How the SDLC Process Changed Software Development?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

Singleton(s) in Java

Singleton — Most Widely misunderstood concept in Spring.

Single Responsibility Principle (SRP)

Summing Java Streams Api

summing java streams api using java 8 streams features