Dec 16 2011

Java library performance optimzation using local references to final members: should you use it?

Category: javaUlrich Palha @ 9:37 am

Mysterious java idiom

While I was browsing through the java.util.concurrent source code, I noticed widespread use of the following idiom

from java.util.concurrent.ThreadPoolExecutor
1
2
3
4
5
6
7
8
9
10
private final ReentrantLock mainLock = new ReentrantLock();
    ...
    //local reference to final member variable
    final ReentrantLock mainLock = this.mainLock;
    mainLock.lock();
    try {
        ...    
    } finally {
        mainLock.unlock();
}

Given that member mainLock is final, I could not understand the benefit of declaring a local reference. Continue reading “Java library performance optimzation using local references to final members: should you use it?”

 

 

Tags: , , ,