Monday, September 29, 2008

What do synchronized functions mean exactly?

I was trying to write thread-safe code on Java 1.4 with its pre-JSR 133 broken memory model and everything, and I thought synchronized function just synchronized that function, but it doesn't! Synchronized function synchronizes across all synchronized functions of the same instance.


synchronized void function()
{
int two = 1+1;
}

is really just short for

void function()
{
synchronized(this)
{
int two = 1+1:
}
}

same thing with synchronized static functions, except they synchronize on Foo.class

No comments: