[concurrency-interest] general performance question

Gregg Wonderly gregg at cytetech.com
Thu Dec 22 09:21:18 EST 2011


On 12/22/2011 3:32 AM, Roel Spilker wrote:
> You can use http://projectlombok.org/features/Synchronized.html as a way to use
> a private lock object AND (potentially) have static analysis.
>
> Can anyone explain to me why an overridden method should be synchronized as well?

It really depends on what the overridden method does.  Sometimes, what I do, is 
create a subclass and do something like the following to investigate contention. 
  In this case, I want every thread instance to enter, unblocked, so that I can 
count who all is waiting.

public class SubFoo extends Foo {
	AtomicInteger ecnt = new AtomicInteger(0);

	public void fooMethod() {
		int mine = ecnt.incrementAndGet();
		// track max value here too, and log when it grows.
		try {
			super.fooMethod();
		} finally {
			ecnt.decrementAndGet();
		}
	}
}

> Roel
>
> Disclosure: I'm involved in Project Lombok

As an aside...  On the @Synchronized page, the example shown, uses

	Object lock = new Object[0];

which could also be

	Serializable lock = new Serializable(){};

to make serialization work as well.  This documents, with code, what the desired 
attribute really is.  The anonymous inner class, of course will create extra 
compiler output in the form of class files, and extend the number of classes 
loaded, etc.

Regarding serialVersionUID changing, you could use an additional annotation that 
created a single field for all locks stored in a Map so that there was only a 
single "field" present for lock support.  Then, you'd have to remove that 
annotation to "change" the serialVersionUID, and the number of locks would not 
affect or change serialVersionUID. All the synchronized() use would now be 
synchronized(map.get(lockKey)) instead.

What this facilitates, is that now you have a functional interface to managing 
lock access, and you can add some telemetry as well, by generating code such as

	synchronized( map.get(lockKey) ) {
		try {
			...users code...
			map.entered( lockKey );
		} finally {
			map.exited( lockKet );
		}
	}

You can provide a lot of extra features once the lock access happens through an 
object that has keyed access.

Also, does lombok work in netbeans?

Gregg Wonderly

> On 21-12-2011 17:45, Nathan Reynolds wrote:
>> On the other hand, "synchronized(this)" will trigger Eclipse or FindBugs to
>> warn you that the code is synchronizing on a publicly accessible object. This
>> means that if another piece of code synchronizes on the object then there
>> might be an unintended scalability problem.
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>
>



More information about the Concurrency-interest mailing list