小编use*_*415的帖子

为什么Java编译器在“同步块”之前添加“冗余读取”?

// the java source code
public class Demo {
    private final Object lock = new Object();

    public void read() {
        synchronized (lock) {
            // more code here ...
        }
    }
}

// the decompiled .class file
public class Demo {
    private final Object lock = new Object();

    public void read() {
    // Why Java compiler add this line? Is the 'read this.lock' redundant?
    Object var1 = this.lock;
    synchronized(this.lock) {
        // more code here ...
    }
    }
}
Run Code Online (Sandbox Code Playgroud)

字节码在这里:javap …

java bytecode synchronized

2
推荐指数
1
解决办法
157
查看次数

标签 统计

bytecode ×1

java ×1

synchronized ×1