零长度阵列的JVM优化 [英] JVM optimisation of zero-length arrays

查看:147
本文介绍了零长度阵列的JVM优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为特定类型的所有零长度阵列是相同的,并在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆