I am reading Java source code, encounter two little questions. I don't know why Sun write code like this, here are questions (JDK version:1.6):
Question 1:
java.util.concurrent.PriorityBlockingQueue#offer(E e):
public boolean offer(E e) {
final ReentrantLock lock = this.lock;
lock.lock();
try {
boolean ok = q.offer(e);
assert ok;
notEmpty.signal();
return true;
} finally {
lock.unlock();
}
}
Why to define a local final ReentrantLock
variable in method block, why not directly to use a global variable lock
?
Question 2:
This method java.text.NumberFormat#getInstance(Locale desiredLocale, int choice):
creates a DecimalFormat
object, but a return type is NumberFormat
. Why not to make return type as DecimalFormat
?