零长度阵列的JVM优化 [英] JVM optimisation of zero-length arrays
问题描述
作为特定类型的所有零长度阵列是相同的,并在JVM优化零长度数组分配使用一个共享实例为所有这种分配
As all zero-length arrays of a particular type are identical, does the JVM optimise allocation of zero-length arrays to use one shared instance for all such allocations?
推荐答案
没有。并且它可以不这样做,因为每个所创建的零长度数组对象可以作为一个不同的同步监视器。 答曰甲骨文:
No. And it may not do so, because each created zero-length array object could be used as a different synchronization monitor. Quoth Oracle:
同步是围绕被称为内在锁或监视器锁定内部实体建...
每个对象具有与之相关联的特性的锁
Synchronization is built around an internal entity known as the intrinsic lock or monitor lock... Every object has an intrinsic lock associated with it.
例如,在下面的code,不同的线程可以调用 x.methodA()
和 x.methodB()
不阻塞。
For example, in the following code, different threads could call x.methodA()
and x.methodB()
without blocking.
private final int[] a;
private final int[] b;
Ctor(int n) {
a = new int[n];
b = new int[n];
}
public final methodA() {
synchronized(a) {
...
}
}
public final methodB() {
synchronized(b) {
...
}
}
这篇关于零长度阵列的JVM优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!