articles

Home / DeveloperSection / Articles / Synchronization in Java

Synchronization in Java

Anupam Mishra3057 04-Nov-2015
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:
publicclassSynchronizationEx
{
privateint x;
privateint 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.

Updated 14-Dec-2017

Leave Comment

Comments

Liked By