Users Pricing

articles

home / developersection / articles / synchronization in java

Synchronization in Java

Anupam Mishra 3531 04 Nov 2015 Updated 14 Dec 2017
It will only class level variable or data can be shared .We can create single object for each thread but we have some condition where we have to share the same object with all threads. So that synchronization is necessary between threads.
For Example:
public class SynchronizationEx
{
private int x;
private int y;
synchronized int(int a,int b)
{
this.x=a;
this.y=b;
return x+y;
}
}

In java every object  is having a one implicit lock on it i.e. called lock monitor.
If any thread want to run any method then it have to get that lock and only one thread can enter in synchronized method.
Synchronization  degrade the performance of thread.