|
Cost of Synchronization
Listing 1. VMs have cheap synchronization for unlocked objects, but the cost may be worse than you expect because threading issues interfere with other taken-for-granted optimizations. /**
* Illustrates overhead of method
* synchronization.
*/
class Test1
{
private int a, b, c;
/**
* Setter for a single attribute.
*/
public synchronized void setA (int a)
{
this.a = a;
}
/**
* Setter for multiple attributes.
*/
public synchronized void f (int b, int c)
{
this.b = b;
this.c = c;
}
public static void main(String[] args)
{
Test1 test1 = new Test1();
test1.setA(10);
test1.f(20, 30);
}
}
|