What is Synchronization in Java
Synchronization in Java is the capability to restrict or control the access of multiple threads to any shared resource.
Java Synchronization is better option where we want to allow only one thread to access the shared resource.
We will first see how Threads perform if we don't use Synchronized Keyword and then how it performs using Synchronized Keyword.
- Without Synchronized Keyword:
Steps we will be performing-
- Create a non- synchronized method where you print threads with the interval of 400 ms.
- Create two threads and call that non-synchronized methods.
- Call start method with two threads inside main method.
Create Non-Synchronized method:
Create a non-Synchronized method named as printable(int n) where we need to display threads within 400 ms.
void printTable(int n) { //method not synchronized
for (int i = 1; i <= 5; i++) {
System.out.println(n * i);
try {
Thread.sleep(400);
} catch (Exception e) {
System.out.println(e);
}
}
}
Create MyThread1:
Create MyThread1 as a child class of thread where call non-synchronized methods printable(5) and create a constructor also.
class MyThread1 extends Thread {
Table t;
MyThread1(Table t) {
this.t = t;
}
public void run() {
t.printTable(5);
}
}
Create MyThread2:
Create MyThread2 as a child class of thread where call non-synchronized methods printable(100) and create a constructor also.
class MyThread2 extends Thread {
Table t;
MyThread2(Table t) {
this.t = t;
}
public void run() {
t.printTable(100);
}
}
Create Thread objects and call start() method for every threads:
Finally inside main create objects of thwo threads and call start() method to start all the threads using non-synchronized method.
Table obj = new Table(); //only one object
MyThread1 t1 = new MyThread1(obj);
MyThread2 t2 = new MyThread2(obj);
t1.start();
t2.start();
Full Java Code for without Synchronized Keyword:
class Table {
void printTable(int n) { //method not synchronized
for (int i = 1; i <= 5; i++) {
System.out.println(n * i);
try {
Thread.sleep(400);
} catch (Exception e) {
System.out.println(e);
}
}
}
}
class MyThread1 extends Thread {
Table t;
MyThread1(Table t) {
this.t = t;
}
public void run() {
t.printTable(5);
}
}
class MyThread2 extends Thread {
Table t;
MyThread2(Table t) {
this.t = t;
}
public void run() {
t.printTable(100);
}
}
class TestSynchronization1 {
public static void main(String args[]) {
Table obj = new Table(); //only one object
MyThread1 t1 = new MyThread1(obj);
MyThread2 t2 = new MyThread2(obj);
t1.start();
t2.start();
}
}
So if you see above output, you can see both Threads are accessing Print Resource. One thread writes something and then goes to sleep. At that time, Second Thread awakes and writes something. So this goes on and on until termination. So the Resource is not restricted and can cause Data Issues for Applications like Bank Management, etc.
- With Synchronized Keyword
Now we will be using Synchronized keyword. This is useful when you want to restrict access to particular resource by only 1 Thread at a time. Like for example, while performing Balance deduction, you should only allow 1 Thread or else it will Inconsistent Balance Problem in Database.
Steps we will be performing-
- Create a synchronized method where you print threads with the interval of 400 ms.
- Create two threads and call synchronized methods.
- Call start method with two threads inside main method.
Create Synchronized method:
Create a Synchronized method named as printable(int n) where we need to display threads within 400 ms.
synchronized void printTable(int n) { //synchronized method
for (int i = 1; i <= 5; i++) {
System.out.println(n * i);
try {
Thread.sleep(400);
} catch (Exception e) {
System.out.println(e);
}
}
}
Create MyThread1:
Create MyThread1 as a child class of thread where call synchronized methods printable(5) and create a constructor also.
class MyThread1 extends Thread {
Table t;
MyThread1(Table t) {
this.t = t;
}
public void run() {
t.printTable(5);
}
}
Create MyThread2:
Create MyThread2 as a child class of thread where call non-synchronized methods printable(100) and create a constructor also.
class MyThread1 extends Thread {
Table t;
MyThread1(Table t) {
this.t = t;
}
public void run() {
t.printTable(5);
}
}
Create Thread objects and call start() method for every threads:
Finally inside main create objects of two threads and call start() method to start all the threads using non-synchronized method.
Table obj = new Table();//only one object
MyThread1 t1=new MyThread1(obj);
MyThread2 t2=new MyThread2(obj);
t1.start();
t2.start();
Full Java Code:
class Table {
synchronized void printTable(int n) { //synchronized method
for (int i = 1; i <= 5; i++) {
System.out.println(n * i);
try {
Thread.sleep(400);
} catch (Exception e) {
System.out.println(e);
}
}
}
}
class MyThread1 extends Thread {
Table t;
MyThread1(Table t) {
this.t = t;
}
public void run() {
t.printTable(5);
}
}
class MyThread2 extends Thread {
Table t;
MyThread2(Table t) {
this.t = t;
}
public void run() {
t.printTable(100);
}
}
public class TestSynchronization2 {
public static void main(String args[]) {
Table obj = new Table(); //only one object
MyThread1 t1 = new MyThread1(obj);
MyThread2 t2 = new MyThread2(obj);
t1.start();
t2.start();
}
}