victor yao
victor yao

Reputation: 89

Referencing class variables from methods using extra local variables

I was reading the ArrayBlockingQueue implementation code another day by Doug Lea and noticed a lot of methods (public, default, and private) have the following references:

final Object[] items = this.items;
final ReentrantLock lock = this.lock;

I have asked around to have a reasonable explanation but so far no satisfactory answers. I am not quite sure why we need to have such local variables in the first place? And what is the benefit(s) of coding this way?

Maybe I missed some important points in concurrent programming. Could you please help to shed some lights on this?

Upvotes: 4

Views: 138

Answers (3)

victor yao
victor yao

Reputation: 89

After going through all relevant threads on the coding practice of assigning a final class variable to a local copy, i.e. a final class variable is never accessed directly from within a method, instead it is always referenced by a local variable reference:

final Object[] items = this.items;

final ReentrantLock lock = this.lock;

Typically you will find this code style in ArrayBlockingQueue and other concurrent classes

The following are my findings:

  • It is an idiomatic use, made popular by Doug Lea, the main author of the core Java library on multithreading/concurrency classes
  • The main consideration of this coding practice (or rather a hack) is for a small performance optimization back in Java 5 era
  • It is arguable if such a trick can have a performance gain; Some argued it is opposite with modern compiler; Others believe it is not needed

So my takings are that we should not be encouraged to adopt this practice. Because in many applications you don’t need it. Clean code maybe more important than a small performance gain; let alone no one is 100% certain whether this (a performance gain) is the case anymore.

Upvotes: 2

victor yao
victor yao

Reputation: 89

It happened that I just came across this link which explained some of the main arguments of coding this way:[In ArrayBlockingQueue, why copy final member field into local final variable?. Please read it to understand more, instead, I am hoping of not getting more confused. I believe it helps you to look at this practice from another angle. It seems it at least met some of my curiosities around this coding style.

Upvotes: 2

John Bollinger
John Bollinger

Reputation: 180058

A very good reason for a method to set a local variable to the value of an accessible class or instance variable, or a value accessible through one of those, is to thereafter be independent of any modifications to that variable by other threads. With some caveats, this allows the method that needs to access that value more than once to perform a consistent computation reflecting the state of the host object at some specific point in time, even if that state has changed by the time the result is returned. This is likely what's happening in the code you have been studying.

Upvotes: 3

Related Questions