In java,If we declare any method as synchronized (keyword) then this method will be known as synchronized method and this method is used to lock an object for any shared resource.When a thread invokes a synchronized method, it automatically acquires the lock for that object and releases it when the thread completes its task.Example:
public class SynchronizedCounter {
private int c = 0;
public synchronized void increment() {
c++;
}
public synchronized int value() {
return c;
}
}
and Synchronized Statement executes when the thread has obtained a lock for the object or the class that has been referred to in the statement. Synchronized statement contains a synchronized block, within which is placed objects and methods that are to be synchronized.See this example:
int count=0;
public void concatenateString(String string)
{
synchronized(this)
{ // Synchronized Statement
stringOne= string;
count++;
}
stringList.concatenateString(string);
}