AutoLock
Dr Heinz M. Kabutz
heinz at javaspecialists.eu
Sun Feb 27 11:30:01 PST 2011
Hi,
Brian told me about the discussion going on here on coin-dev. I
published a newsletter today on the new Java 7 try-with-resource
construct and how we could use it for automatically unlocking. By
combining it with static imports, we can write the following code:
try (lock(lock)) {
printLockStatus();
}
Instead of the current more verbose
lock.lock();
try {
printLockStatus();
} finally {
lock.unlock();
}
In my newsletter, I expressed the same concern as Neal, that
constructing objects, and in fact encouraging people to do this in
performance critical code, could be a killer for this idea.
However, initial tests indicate that escape analysis actually does a
nice job of eliminating the cost of the object construction. These are
just completely basic initial tests and are quite possibly flawed.
However, with the default escape analysis turned on, my try (lock(lock))
code did not cause any objects to be created, even though I called it
1_000_000_000 times.
When I turned off escape analysis, it constructed about 30GB of objects.
As I said, I have not tested this properly, just an initial investigation.
Here is the link to my article:
http://www.javaspecialists.eu/archive/Issue190.html
Regards
Heinz
--
Dr Heinz M. Kabutz (PhD CompSci)
Author of "The Java(tm) Specialists' Newsletter"
Sun Java Champion
IEEE Certified Software Development Professional
http://www.javaspecialists.eu
Tel: +30 69 72 850 460
Skype: kabutz
More information about the coin-dev
mailing list