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

Are You a Devops or an SRE?

What is SASS?

SASS

Battleship: Lessons Learned

My First CLI Project

Raml 1.0 in Mulesoft — Basics

How to Choose the Right Fleet Management Software for Your Company

Snake City: Public NFT Sale

Adding Dark Theme to your Website under 10 Seconds

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

Explore Prototype Design Pattern

SOLID Principles in Java

Garbage Collection — Java Architecture

Visitor Design Pattern in Java