What is
synchronization in java?
Synchronization is a mechanism using which we can develop a way by which we
can restrict the access of a resource by multiple threads thereby reducing the
risk of deadlock conditions.
Lock in java
Synchronization uses locking technique which is built around an internal entity
known as lock or monitor. In synchronization, whenever a thread needs access to
a resource, it must acquire the lock on the object. After the process is
completed the thread must release the lock.
Why we use
synchronization?
Synchronization is used to
1.
Prevent thread interference
2.
Prevent consistency programming.
To understand this let us create a program which will print the numbers
in increment order by using two threads.
In the above example, we can see that there is only one object of class sequence which is shared among the two threads and as there is no synchronization the output will be not as what is desired.
output
5
100
10
200
15
300
20
400
25
500
Now, to overcome this issue we must implement synchronization into the program.
Let us see how we can achieve synchronization and in how many ways.
Types of Synchronization
1.
Process Synchronization
2.
Thread Synchronization
Thread Synchronization
Thread Synchronization is divided into two parts
1.
Mutual Exclusive
a.
Synchronized method
b.
Synchronized block
c.
Static Synchronization
2.
Inter-thread communication
Let’s discuss this one by one.
1.
Mutual Exclusive
This technique of Synchronization restricts the other threads to access
the already locked resource. This can be achieved in three ways:
a.
Synchronized method
If we use the Synchronized keyword before any normal
method, then that method will be said to be a synchronized method.
By making the method as synchronized it is assured that the thread which
calls the synchronized method will acquire the object lock and will keep the object
lock until it’s execution is completed.
Let’s take the above example and try to reconstruct it using method synchronization
and see what will be the output
Here the method printSequence is synchronized, hence one thread will execute the method completely and after that only it will release the lock and gives the turn to next thread. We will get the desired output.
Output
5
10
15
20
25
100
200
300
400
500
b. Synchronized
block
Suppose we have 60
lines of code inside a method but, we want to synchronize only a few resources
in that method. In such a case, synchronizing the whole method will not be
feasible. Hence, we have the concept of synchronized block.
Important points
1.
The synchronized block has less scope as compared to
method synchronization.
2.
Block level synchronization is more preferable than
method level synchronization because making the whole method synchronized
rather than a particular resource will slow the performance.
3.
The scope of lock will be block level.
Let us take the same example of sequence class and this time we will make use of block synchronization.
Observe in the above example we have created a synchronized
block and removed the synchronized method. The output will be the same, but the
performance will be much faster.
Output
5
10
15
20
25
100
200
300
400
500
c. Static
synchronization
Consider the scenario where there are two objects of a shared class. During
method synchronization and block
synchronization, there will be no interference between thread1 and thread2, thread3 and
thread4. But between thread1 and thread3 there can be some interference similar
is the case with thread2 and thread4.
To overcome this situation static synchronization comes into the picture. The static
synchronization will acquire the lock on class and not on the object. Consider the following
example below.
2.
Inter-thread communication
For this topic, I
have a separate description. Please check out Multithreading concept in java.
That is all for this tutorial. I recommend reading the Multithreading
concept after or before this article. Also, you can check out the related
articles below. Please feel free to fill the gaps in this article if any.
0 Comments