java - In ArrayBlockingQueue, why copy final member field into local final variable? -
in arrayblockingqueue, methods require lock copy local final variable before calling lock().
public boolean offer(e e) { if (e == null) throw new nullpointerexception(); final reentrantlock lock = this.lock; lock.lock(); try { if (count == items.length) return false; else { insert(e); return true; } } { lock.unlock(); } } is there reason copy this.lock local variable lock when field this.lock final?
additionally, uses local copy of e[] before acting on it:
private e extract() { final e[] items = this.items; e x = items[takeindex]; items[takeindex] = null; takeindex = inc(takeindex); --count; notfull.signal(); return x; } is there reason copying final field local final variable?
it's extreme optimization doug lea, author of class, likes use. here's post on a recent thread on core-libs-dev mailing list exact subject answers question pretty well.
from post:
...copying locals produces smallest bytecode, , low-level code it's nice write code that's little closer machine
Comments
Post a Comment