在ArrayBlockingQueue,所有需要锁的方法final在调用之前将其复制到局部变量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;
}
} finally {
lock.unlock();
}
}
Run Code Online (Sandbox Code Playgroud)
当字段是什么时,有没有理由复制this.lock到局部变量?lockthis.lockfinal
此外,它还在使用E[]之前使用本地副本:
private E extract() {
final E[] items = this.items;
E x = items[takeIndex];
items[takeIndex] = null;
takeIndex = inc(takeIndex);
--count;
notFull.signal();
return x;
}
Run Code Online (Sandbox Code Playgroud)
有没有理由将最终字段复制到本地最终变量?
这是来自JDK的HashMap类的keySet()方法.为什么作者将字段(keySet)分配给局部变量ks?
public Set<K> keySet() {
Set<K> ks;
return (ks = keySet) == null ? (keySet = new KeySet()) : ks;
}
Run Code Online (Sandbox Code Playgroud)
上面和下面有什么区别?这与线程安全有关吗?
public Set<K> keySet() {
return (keySet == null ? (keySet = new KeySet()) : keySet;
}
Run Code Online (Sandbox Code Playgroud) 在OpenJDK 8中探索java.util.LinkedList的代码,我找到了以下代码.代码很简单,但我很困惑将第一个节点的引用保存到第二行代码中的常量.据我所知,这段代码将被内联到单行内容而无需参考复制.我对吗?如果是这样,为什么需要在这个和类似的情况下复制引用(这样的习惯用法可以在java.util.LinkedList的一半方法中找到)?
public E peek() {
final Node<E> f = first;
return (f == null) ? null : f.item;
}
Run Code Online (Sandbox Code Playgroud)
我的第一个想法是它以某种方式帮助并发,但LinkedList不允许并发访问(除了你自己承担风险),所以我想这是优化器的一些提示,但无法弄清楚它应该如何工作.
我是编程和Java的新手.我注意到,在Java API中,if语句中有一些奇怪的赋值方法.
以下是Map界面的示例:
default V replace(K key, V value) {
V curValue;
if (((curValue = get(key)) != null) || containsKey(key)) {
curValue = put(key, value);
}
return curValue;
}
Run Code Online (Sandbox Code Playgroud)
以这种方式嵌套赋值是否有某种好处?这纯粹是一种风格选择吗?为什么不在curValue第一次声明时进行赋值?
// why not do it like this?
default V replace(K key, V value) {
V curValue = get(key); // not nested
if (curValue != null || containsKey(key)) {
curValue = put(key, value);
}
return curValue;
}
Run Code Online (Sandbox Code Playgroud)
我在Map界面和其他地方的许多新添加的Java 8方法中都注意到了这一点.这种嵌套分配的形式似乎是不必要的.
编辑:Map界面中的另一个示例:
default V computeIfAbsent(K key,
Function<? super K, ? extends …Run Code Online (Sandbox Code Playgroud) 看java的hashmap实现,无法理解一些行背后的原因.在下面从这里复制的代码中,在365-367行中,我无法理解他们为什么先将e.key赋值给k然后将==与key [(k = e.key)== key进行比较].为什么不直接做(e.key == key).此模式在代码中出现多次.
359
360 final Entry<K,V> getEntry(Object key) {
361 int hash = (key == null) ? 0 : hash(key.hashCode());
362 for (Entry<K,V> e = table[indexFor(hash, table.length)];
363 e != null;
364 e = e.next) {
365 Object k;
366 if (e.hash == hash &&
367 ((k = e.key) == key || (key != null && key.equals(k))))
368 return e;
369 }
370 return null;
371 }
Run Code Online (Sandbox Code Playgroud) 这是什么的OpenJDK线的优点数1455.
代码段:
private final char value[];
// ...
public int hashCode() {
int h = hash;
if (h == 0 && value.length > 0) {
char val[] = value; // <--- this line
for (int i = 0; i < value.length; i++) {
h = 31 * h + val[i];
}
hash = h;
}
return h;
}
Run Code Online (Sandbox Code Playgroud)
请注意,虽然参照private final char value[]被复制到本地val的循环内访问,其.length领域还在通过访问value,不是val.
我怀疑" 性能 "是答案(例如,从本地读取比从现场读取更快)但我会欣赏一个精确且易于阅读的答案,甚至可能有一些关于优势的数据.