This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 175734 - Warn about synchronization on multiple objects
Summary: Warn about synchronization on multiple objects
Status: NEW
Alias: None
Product: java
Classification: Unclassified
Component: Hints (show other bugs)
Version: 6.x
Hardware: All All
: P3 blocker (vote)
Assignee: Svata Dedic
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-10-30 03:20 UTC by _ tboudreau
Modified: 2013-09-02 14:20 UTC (History)
0 users

See Also:
Issue Type: ENHANCEMENT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description _ tboudreau 2009-10-30 03:20:49 UTC
There is a common category of synchronization bug, where you start with some code
public void setFoo(Foo foo) {
   Foo old;
   synchronized (this) {
      old = this.foo;
      this.foo = foo;
   }
}
and then you realize you need to do it in a mutex, or on a background thread.  So you change it to, say
public void setFoo(Foo foo) {
   ProjectManager.mutex().postWriteAccess(new Runnable() {
       public void run() {
           synchronized (this) {
           old = this.foo;
           this.foo = foo;
       }
   });
}
(okay, it's a slightly artificial example; issue 149298 is a real one)

What you do not notice is that synchronized(this) is now synchronizing on a Runnable, not the outer class.

What would be nice is to
1.  Find all fields which are only set in a synchronized block
2.  Detect if not all the synchronized blocks use the same monitor, and warn/offer to fix (fixing might be harder, but 
the warning at least could be useful)

Seems like a real, easy to create bug where an IDE is more likely to catch it than a human.