Synchronization :
Two or more threads trying to access the same method at the same point of time leads to synchronization. If that method is declared as Synchronized, only one thread can access it at a time. Another thread can access that method only if the first thread's task is completed.
public synchronized void Method () {
public myFunction (){
Two or more threads trying to access the same method at the same point of time leads to synchronization. If that method is declared as Synchronized, only one thread can access it at a time. Another thread can access that method only if the first thread's task is completed.
Example:
Synchronizing a function:
Synchronizing a function:
public synchronized void Method () {
// Appropriate method-related code.
}
Example:
Synchronizing a block of code inside a function:
Synchronizing a block of code inside a function:
public myFunction (){
synchronized (this) {
// Synchronized code here.
}