synchronized method vs synchronized block

View previous topic View next topic Go down

synchronized method vs synchronized block

Post  Admin on Fri Oct 14, 2011 6:32 pm

Both “synchronized block” and “synchronized method” provide synchronisation, that is, only one thread can access the object’s synchronised area at one time. The next thread is placed on a queue and access is granted when the previous thread is done working. The queue is not served in a first in first out manner and the dispatcher can order the threads as it see fits (mainly based on their priority and availability amongst other things).

The synchronized method approach covers the method(s) it is applied to as shown in the example below. As the name implies, the smallest unit of work is a method and the whole method is synchronised.

class X {
public void m1(){}
public synchronised void m2(){}
public synchronised void m3(){}
}

Methods m2() and m3() can be accessed by one thread at one time. Any subsequent threads will be placed on a queue and access is only granted to these methods once the previous thread is done with the synchronised method area. Note that if thread A access method m2(), thread B cannot access method m3() until thread A is done with m2(). Method m1() is not synchronised and thus it can be accessed by several threads at the same time.

The synchronized block takes the form of:

synchronized(instance-of-object){
statements to be synchronised – accessed by one thread
}

and can be used to synchronise parts of a method as shown below. One can say that the synchronized block provide more granular control on which parts are to be synchronised and which not. This is useful as in many cases we only need to synchronise parts of the method and not the entire method (as we see in the next code fragment).

private Rectangle r = new Rectangle();

public void m4(){
r.width = 200; // Not synchronised
synchronized(this){
int x = r.x; // These three statements are synchronised
x += 10;
r.x = x;
}
r.y = 100; // Not synchronised
}

Method m4() contain parts which are synchronised and parts that are not. Again, like before, the synchronised parts can be accessed by one thread at one time while the other parts are free for all (so to say). Why is this useful? It is very useful as only the thread sensitive statements are synchronised, while the thread non sensitive (they are not prone to errors due to concurrency – multiple threads executing them at the same time). This would improve performance and synchronisation is expensive.

Furthermore, let say that we want to have method m1() synchronised but we cannot change the code of class X (shown above). We can use synchronised block to achieve this as shown in the code fragments below.

public void m5(){
synchronise(this){m1();}
}

Or

public void m6(){
X x = new X();
synchronise(x){x.m1();}
}

In both example, the access to method m1() is synchronised. The method itself is not, but when accessed through either methods m5() or m6() it become synchronised and only accessed by one thread at one time.

Admin
Admin

Posts : 131
Join date : 2011-08-16

View user profile http://codefornongeek.forumotion.com

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum