旧版套餐怎么购买,我一领取新购卷就显示下面那张图
像是编译器安排了一个类级位图int字段,将多个延迟字段标记为初始化(或未初始化),并在同步块中初始化目标字段(如果位图的相关xor表明必要的话)。
使用:
class Something {
lazy val foo = getFoo
def getFoo = "foo!"
}
生成示例字节码:
0 aload_0 [this]
1 getfield blevins.example.Something.bitmap$0 : int [15]
4 iconst_1
5 iand
6 iconst_0
7 if_icmpne 48
10 aload_0 [this]
11 dup
12 astore_1
13 monitorenter
14 aload_0 [this]
15 getfield blevins.example.Something.bitmap$0 : int [15]
18 iconst_1
19 iand
20 iconst_0
21 if_icmpne 42
24 aload_0 [this]
25 aload_0 [this]
26 invokevirtual blevins.example.Something.getFoo() : java.lang.String [18]
29 putfield blevins.example.Something.foo : java.lang.String [20]
32 aload_0 [this]
33 aload_0 [this]
34 getfield blevins.example.Something.bitmap$0 : int [15]
37 iconst_1
38 ior
39 putfield blevins.example.Something.bitmap$0 : int [15]
42 getstatic scala.runtime.BoxedUnit.UNIT : scala.runtime.BoxedUnit [26]
45 pop
46 aload_1
47 monitorexit
48 aload_0 [this]
49 getfield blevins.example.Something.foo : java.lang.String [20]
52 areturn
53 aload_1
54 monitorexit
55 athrow
以元组开头的值,如lazy val (x,y) = { ... }
通过相同的机制进行嵌套缓存。元组结果是延迟计算和缓存的,对x或y的访问将触发元组计算。从元组中提取单个值是独立和懒惰地进行的(并缓存)。因此,上述双实例化代码将生成一个x
,y
和一个x$1的
Tuple2
类型域
这是从Scala邮件列表的实现细节。lazy
在Java代码方面(而不是字节码):
class LazyTest {
lazy val msg = "Lazy"
}
编译成与以下Java代码等效的内容:
class LazyTest {
public int bitmap$0;
private String msg;
public String msg() {
if ((bitmap$0 & 1) == 0) {
synchronized (this) {
if ((bitmap$0 & 1) == 0) {
synchronized (this) {
msg = "Lazy";
}
}
bitmap$0 = bitmap$0 | 1;
}
}
return msg;
}
}