Tuesday, December 8, 2009


Java: Synchronized

Every JAVA developer may be heard on and off about this word from JDK 1.0 . And, still there are lots of JAVA developers go with the myth.

JAVA is multithreaded language, where any block of code or any method can be run simultaneously by different Thread(s).

Synchronized keyword used in method signature to specify that this method can be run single thread at a time.

Is this enough to filter that all the thread in accessing particular method. No, we have to obtain lock for the particular resource, then only we can really implement filtering/preventing

simultaneous access.

Object lock : If synchronized keyword used in non-static methods in a class, then the object created from this class lets threads to access this method one by one on this object. For

instance, two object created from this class then T1 access obj1.getMethod() and T2 access obj2.getMethod(), this will run parallelly, since these two are different objects.

Class level Lock: If synchronized keyword used in stato method in a class then class level lock will be obtained. At a single point in time , only one static method can be accessed.

If I have a class with one static and another non-static method with synchronized keyword, then these two methods possibly run in parallel.

Above defined synchronized keyword helps in usecase of preventing simultaneous access of instance/class level variables. In real time, we are not required to block all the variable

access, and which will lead to get performance bottleneck. To avoid this mutex approach has to be used, means, that only the lockable object has to be locked instead of the whole

object and class level lock. For this synchronized block has used.

//do single threaded activity

The above code does the job equivalent to synchronized keyword in methods. However, this will perform better than the synchronized keyword, since the call waits after the context

switching of the method, but earlier method entry itself limited and filtered.

In the below code, static and non-statice present with synchronized keyword, and both method shares the same variable ind and almost all the time ind value as 20 in last line of

output. However, there could be possibility of getting some other number due to ind variable is not a volatile. I will explain in forthcoming blogs about volatile keyword.

public class Sync {
static int ind = 0;
public synchronized void wow() {
for (int i = 1; i <= 10; i++) {
System.out.println("hello " + ++ind);
try {
} catch (Exception e) {
System.out.println("kris " + ind);


public static void wow1() {
for (int i = 1; i <= 10; i++) {
System.out.println("wow " + ++ind);
try {
} catch (Exception e) {
System.out.println("d " + ind);

public static void main(String[] args) {
final Sync s = new Sync();
Thread t1 = new Thread() {
public void run()

Thread t2 = new Thread() {
public void run() {



Additionally, If you want to test only object lock then in replace run method of T2 in T1.

1. synchronized keyword will not be used in constructor, since object creation will not be ever thread safe in JAVA.
2. Assume that 100 threads are trying to access the same synchronized block/method, no guarantee in order to get lock to run.
3. By calling wait() method, currently object lock holding thread can release the resource. Same thread will be try to acquire the lock if notify() or notifyAll() method in the same object

get called by the other RUNNING thread, which holds lock. No gurantee in order of which thread calls wait(), to obtain lock again.

java.util.concurrent package has the implementation to guarantee the order of the thread waits in obtaining lock for read and write.

@See java.util.concurrent.locks.ReentrantReadWriteLock

1 comment:

JAvin @ HashTable vs HashMap in Java said...

Nice article , you have indeed cover the topic with great details. I have also blogged my experience on java How Synchronization works in Java. let me know how do you find it.

Post a Comment

Recent Posts

Unix Commands | List all My Posts


This blog intended to share the knowledge and contribute to JAVA Community such a way that by providing samples and pointing right documents/webpages. We try to give our knowledege level best and no guarantee can be claimed on truth. Copyright and Terms of Policy refer blogspot.com