Introduction
In this article, we discuss Synchronization in Java.
Synchronization
Synchronization is the capability of controlling access of multi-threads to any shared resources. It is better for when we want only one thread to access the shared resource at a time.
Purpose of using Synchronization in Java
Synchronization is mainly used to provide safety for:
- thread interference.
- consistency problem.
Types of Synchronization
The following are the two types of synchronization:
- thread synchronization
- process synchronization
In this article, we discuss only thread synchronization.
Thread Synchronization
There are of two types of thread synchronization, inner-thread (cooperation) and mutually exclusive communication.
Mutually exclusive
Mutually exclusive synchronization helps threads to communicate while sharing common data. That can happen in Java by one of the three ways, with the use of a synchronized block, static synchronization and a synchronized method.
What is lock?
Synchronization is built around an internal entity known as the lock or monitor. Every object has a lock associated with it. By convention, a thread that needs consistent access to an object's fields needs to acquire the object's lock before accessing them, and then release the lock when it's done with them.
Without the use of synchronization take a problem
In this example, we don't use any synchronization, so the output is inconsistent. Let's have a look.
- class Chair
- {
- void printChair(int i)
- {
- for (int x = 1; x <= 6; x++)
- {
- System.out.println(i * x);
- try
- {
- Thread.sleep(350);
- }
- catch(Exception ey)
- {
- System.out.println(ey);
- }
- }
- }
- }
- class Thread1 extends Thread
- {
- Chair ch;
- Thread1(Chair ch)
- {
- this.ch = ch;
- }
- public void run()
- {
- ch.printChair(6);
- }
- }
- class Thread2 extends Thread
- {
- Chair ch;
- Thread2(Chair ch)
- {
- this.ch = ch;
- }
- public void run()
- {
- ch.printChair(250);
- }
- }
- class Print
- {
- public static void main(String args[])
- {
- Chair obj = new Chair();
- Thread1 th1 = new Thread1(obj);
- Thread2 th2 = new Thread2(obj);
- th1.start();
- th2.start();
- }
- }
Output
Solution of the previous problem using Synchronization
- Define the method as synchronized.
- This synchronized method is used to lock an object for any shared resource.
- When our thread extends a synchronized method, it contains that lock for the object and releases the lock when the method returns.
- class Chair
- {
- synchronized void printChair(int i)
- {
- for (int x = 1; x <= 6; x++)
- {
- System.out.println(i * x);
- try
- {
- Thread.sleep(350);
- }
- catch (Exception ey)
- {
- System.out.println(ey);
- }
- }
- }
- }
- class Thread1 extends Thread
- {
- Chair ch;
- Thread1(Chair ch)
- {
- this.ch = ch;
- }
- public void run()
- {
- ch.printChair(6);
- }
- }
- class Thread2 extends Thread
- {
- Chair ch;
- Thread2(Chair ch)
- {
- this.ch = ch;
- }
- public void run()
- {
- ch.printChair(250);
- }
- }
- class Print
- {
- public static void main(String args[])
- {
- Chair obj = new Chair();
- Thread1 t1 = new Thread1(obj);
- Thread2 t2 = new Thread2(obj);
- t1.start();
- t2.start();
-
- }
- }
Output