Professional Documents
Culture Documents
Synchorisation in Java
Synchorisation in Java
Synchorisation in Java
In this post, we will learn synchronization in java, and why we need Java
synchronization, how to write synchronized code and more important points
about synchronization.
Here is the table content of the article will we will cover this topic.
1. Synchronization in java?
2. Why do we use Synchronization in Java?
3. Locks in Java?
4. Types of Synchronization?
5. Multi-threading without Synchronization?
6. Multi-threading with Synchronization?
7. Important points about java synchronized?
8. Java synchronized block
i) synchronized block in non-static method
ii) synchronized block in static method
9. Java synchronized method
i) Java synchronization with non-static method
ii) Java synchronized method
Synchronization in java
Suppose you have two resources and there may be a chance when multiple
threads try to attempt the same resource and produce errors. So
synchronization is used to handle the following errors:
Locks in Java
Types of Synchronization
There are 2 types of synchronization in java as shown below:
Let’s discuss an example that prints the count by two different threads.
class Count
{
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
{
Count c;
MyThread1(Count c)
{
this.c=c;
}
public void run()
{
c.printTable(5);
}
}
class MyThread2 extends Thread
{
Count c;
MyThread2(Count c)
{
this.c=c;
}
public void run()
{
c.printTable(100);
}
}
public class WithoutSynchronization
{
public static void main(String args[])
{
Count obj = new Count();//only one object
MyThread1 t1=new MyThread1(obj);
MyThread2 t2=new MyThread2(obj);
t1.start();
t2.start();
}
Output: 5
100
200
10
15
300
400
20
500
25
{
//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);}
Count c;
MyThread1(Count c)
{
this.c=c;
}
public void run()
{
c.printTable(5);
}
}
class MyThread2 extends Thread
{
Count c;
MyThread2(Count c)
{
this.c=c;
}
public void run()
{
c.printTable(100);
}
}
public class WithSynchronization
{
public static void main(String args[])
{
Count obj = new Count();//only one object
MyThread1 t1=new MyThread1(obj);
MyThread2 t2=new MyThread2(obj);
t1.start();
t2.start();
}
Output: 5
10
15
20
25
100
200
300
400
500
2. If the lock is available, the thread takes the lock and performs the operation
as per requirements. Meanwhile, all the other thread waits for the lock.
// method_code
class Test
{
synchronized public void count()
{
for (int i = 0; i < 3; i++)
{
System.out.println(i);
try
{
Thread.sleep(400);
}
catch (Exception e)
{
System.out.println(e);
}
}
}
}
class MyThread extends Thread {
Test test;
MyThread (Test test) {
this.test = test;
}
@Override
public void run() {
test.count();
}
}
public class SynchronizedMethod
{
public static void main(String[] args)
{
Test obj = new Test(); //Object of Apple class that is shared amoung threads
MyThread thread1 = new MyThread(obj);
MyThread thread2 = new MyThread(obj);
thread1.start();
thread2.start();
}
}
Output: 0
1
2
0
1
2
In the above example, we are creating two threads thread1 and thread2. Both
are using the synchronized count() method. As it is synchronized, both are
using it one by one. Firstly, thread1 enters the count() method and gets a
lock meanwhile thread2 waits until thread1 finishes the execution. and
release the lock. After that thread2 gets the lock and completes the execution.
{
// method_code
}
class Test
{
synchronized static public void countIncrement()
{
System.out.println("Increment values");
for (int i = 0; i < 3; i++)
{
System.out.println(i);
try
Thread.sleep(400);
catch (Exception e)
{
System.out.println(e);
}
}
}
synchronized static public void countDecrement()
{
System.out.println("Decrement values");
for (int i = 3; i > 0; i--)
{
System.out.println(i);
try
{
Thread.sleep(400);
catch (Exception e)
{
System.out.println(e);
}
}
}
}
class MyThread extends Thread {
Test test;
MyThread (Test test) {
this.test = test;
}
@Override
public void run() {
test.countIncrement();
test.countDecrement();
}
}
public class SynchronizedMethod
{
public static void main(String[] args)
{
Test obj = new Test(); //Object of Apple class that is shared amoung threads
thread1.start();
thread2.start();
synchronized ( lockObject)
{
//synchronized statements
}
A thread can execute the synchronized block only to acquire the lock on
lockObject. Only one thread can acquire the monitor of a lock object at a time
meanwhile all other threads wait for the lock.
When you want to synchronize a portion of the method instead of the whole
method. We can use a synchronized block of Java code inside an
unsynchronized Java method.
You must think about why the synchronized block construct takes an object in
parentheses. This object is known as the monitor object. Only one thread can
execute inside a Java code block synchronized on the same monitor object.
class Test
{
public void countIncrement()
{
synchronized(this)
{
for (int i = 0; i < 3; i++)
{
System.out.println(i);
try
{
Thread.sleep(400);
}
catch (Exception e)
{
System.out.println(e);
}
}
}
}
}
class MyThread extends Thread {
Test test;
MyThread (Test test) {
this.test = test;
}
@Override
public void run() {
test.countIncrement();
}
}
public class SynchronizedMethod
{
public static void main(String[] args)
{
thread1.start();
thread2.start();
Output: 0
1
2
0
1
2
We can use the Synchronized blocks inside the static method. Suppose if
we have two static methods with a synchronized block then only one
thread can execute inside any of these two methods at the same time.
class Test
{
synchronized static public void countIncrement()
{
synchronized (Test.class)
{
System.out.println("Increment values");
for (int i = 0; i < 3; i++)
{
System.out.println(i);
try
{
Thread.sleep(400);
}
catch (Exception e)
System.out.println(e);
}
}
}
}
synchronized static public void countDecrement()
{
synchronized (Test.class)
{
System.out.println("Decrement values");
for (int i = 3; i > 0; i--)
{
System.out.println(i);
try
{
Thread.sleep(400);
}
catch (Exception e)
{
System.out.println(e);
}
}
Test test;
this.test = test;
@Override
test.countIncrement();
test.countDecrement();
Test obj = new Test(); //Object of Apple class that is shared amoung threads
thread1.start();
thread2.start();