Re: sync on local variable
On 24-03-2010 19:21, Patricia Shanahan wrote:
Arne Vajh?j wrote:
On 24-03-2010 18:11, Roedy Green wrote:
The IntelliJ code Inspector (lint) slapped my wrist for synchronising
on a local variable. What's the problem with that? The sync is on
the object, not the reference, right?
Local variables are thread specific.
Passing on a local variable to another thread via some
global variable/singleton/cache would be a pretty bad design.
I can understand the complaint.
It may be a case of import, rather than export. Suppose the method for
obtaining a reference to some object is fairly expensive, and
synchronized. Each thread could have obtained a reference to the same
object and cached it in a local variable.
It could be.
But I would tend to believe that would be messy as well. To be able to
easily verify that threads are indeed synchronizing on the same object,
then I think it is best if is relative simple to see what is being
synchronized on.
It's difficult to know if the message was valid or not without an SSCCE.
I don't think the tool does super sophisticated code analysis. Most
likely it just have some simple rules and flag things that looks weird.
If they are good enough, then the developer can ignore the warning. And
a good tool would have the option to store the ignore decision.
Arne