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

Summary: Warn about synchronization on multiple objects
Product: java Reporter: _ tboudreau <tboudreau>
Component: HintsAssignee: Svata Dedic <sdedic>
Status: NEW ---    
Severity: blocker    
Priority: P3    
Version: 6.x   
Hardware: All   
OS: All   
Issue Type: ENHANCEMENT Exception Reporter:

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.