Reputation: 3033
This is not a question about method synchronisation, only to statement synchronisation. Please do not incorrectly mark as duplicate.
Is this code:
synchronized (this) if (something)
{
somecode();
somemorecode();
}
equivalent to this code:
if (something)
{
synchronized (this)
{
somecode();
somemorecode();
}
}
Upvotes: 0
Views: 40
Reputation: 1075795
Is this code...equivalent to this code
No, it isn't. In your first example, the if
test is might be inside the synchronized section. In your second example, the if
is outside the synchronized section.
This is a syntax error:
synchronized (this) if (something)
{
somecode();
somemorecode();
}
synchronized
requires a block, per JLS§14.19:
SynchronizedStatement:
synchronized
( Expression ) Block
If it were valid, it would probably be equivalent to:
synchronized (this)
{
if (something)
{
somecode();
somemorecode();
}
}
...but that's pure speculation; if it's not defined by the JLS, who knows what it would be. :-)
Side note: It's synchronized
, not syncronized
or syncronised
.
Upvotes: 2