Java Synchronization
Synchronization in Java:
Java Synchronization is a process of controlling the access of shared resources by the multiple threads in such a manner where only one thread can access one resource at a time. In non synchronized multithreaded application, it is possible for one thread to modify a shared object, while another thread is in the process of using or updating the object's value.
Synchronization prevents such type of data corruption. Synchronization is best use with the Multi-Threading in Java. Synchronization can be done at two levels:
Synchronization prevents such type of data corruption. Synchronization is best use with the Multi-Threading in Java. Synchronization can be done at two levels:
- Synchronizing a function
- Synchronizing a block of code
Please find the sample code below
Synchronizing a function:
Synchronizing a function:
public synchronized void Display()
{
// related code
}
Synchronizing a block of code inside a function:
public myFunction ()
{
synchronized (this)
{
// Synchronized code
}
}